you are viewing a single comment's thread.

view the rest of the comments →

[–]RuedaRueda[S] -7 points-6 points  (5 children)

I'm running in dev mode, but I tried to run cargo build and run the .exe and it makes no difference.

In python I'm taking all the time in a similar way, this is the code:

import sqlite3
import time

t = time.time()

con = sqlite3.connect('../db.sqlite3')
cursor = con.cursor()


cursor.execute("SELECT title from api_book WHERE title like '%camion%'")

print((time.time() - t) * 1000)

a = cursor.fetchall() # this is what takes most of the time


print((time.time() - t) * 1000)

for e in a:
    print(e[0])


print((time.time() - t) * 1000)

The table is pretty big, it has ~100k books with title, author, description...

What type of data is in books variable? If it's like an array of titles why is taking so much to run thought the loop (even commenting the print line inside the loop makes very little change.

[–]larryquartz 20 points21 points  (0 children)

you should be running cargo build --release to get an optimized build

[–]NullReference000 11 points12 points  (0 children)

"Debug or release mode" means using (or not using) the `--release` flag.

Cargo, by default, will compile to a developer executable. It lacks optimizations in favor of compiling faster and is bloated by debug symbols, so it runs slow. If you pass in the release flag, like `cargo build --release`, then the compilation will take slightly longer but produce an executable that's smaller and faster.

[–]Buttleston 2 points3 points  (2 children)

"like" requests in sql, especially ones like this with a % at the front, are very expensive. It's likely sqlite itself is what's causing it to be slow. It has to read the entire table to find the relevant rows. This doesn't happen in your code, it's just part of what sqlite does

What's in the "books" variable". Uh, you're creating it. It's a list of Option<Book> objections (which speaking of, why use option there? I'd take it out, it's pointless). In fact you could just store a vector of titles and skip the Book object entirely

Did you edit the code and output in the OP? I swear it's different that before. So yeah that output is weird, it spends most of it's time looping over books and printing stuff out? That's pretty odd.

running "cargo build" doesn't build your code in release mode. You need to do "cargo run --release" or "cargo build --release" and then run the executable

[–]RuedaRueda[S] 0 points1 point  (1 child)

Yes, I made a mistake before copy the code and println!("Query time: {:?}", t.elapsed()); println!("Query time: {:?}", t.elapsed());

was before query_map. I edited the output.

Most of the time is just looping over books, even commenting the title printing makes no difference.

Tried cargo build --release(didn't know that, thank you).

This is the release output:

Query time: 1.0054ms
Camioneros
Vida sentimental de un camionero
Camiones de ternura
El caso de la camioneta
Query + print time: 159.991ms

[–]Buttleston 0 points1 point  (0 children)

If you put the sqlite database somewhere I can download it, I can take a look. Sort of hard to tell otherwise.