Forums

Code performance (Local Vs PythonAnywhere)

Hi,

My website is some sort of search engine (www.cryptofeesaver.com) and the code runs about 4 to 5 times slower in PythonAnywhere than in my local laptop. A query that takes about 1.5 seconds in my laptop takes about 6 seconds in the server.

All the inputs of the algorithm are stored in a SQLite database (a separate .db file that I set up). I don't take any other input from the Internet or anywhere else, so network issues should not affect performance.

I'm not a professional developer, so I assume the code has a lot of room for improvement, but I would just be happy that it performed as in my local PC.

I've got the Hacker plan, just in case it matters, and I don't have almost any traffic right now.

Thanks in advance!

Filesystem performance is normally slower on PythonAnywhere than it would be on your local machine -- this is because the disks are connected over a network, so that your code can run on different machines at different times.

The best way to get good performance from your site is probably to switch over to using MySQL (or Postgres if you prefer) rather than SQLite -- the MySQL server machines have their own local disks, so they're much faster. It's also more scaleable, so as your traffic grows you'll see less of a slowdown.

Thanks Giles. I'm already working on migrating my SQLite db to MySQL... I'll post here how performance improved once I finish :)

Great! Looking forward to hearing how it goes. (I see you just posted a question in another thread about the move to MySQL -- will answer that there.)

Hi!

I finally managed to migrate my DB to MySQL. Performance did improve... a bit. The algorithm still takes about 3 times more to run on the server (with MySQL) than in my laptop (with SQLite). Is any other reason that comes to your mind that could help me boost performance?

The algorithm makes a bunch of small queries to the database during its execution (imagine a flight search engine algorithm that searches and combines all possible flights to get all the combinations to get to a destination). Maybe I just need to tweak MySQL parameters so that it performs better with this kind of of small sequential queries?

Or maybe there is another reason that has nothing to do with the database...

Thanks in advance

Hmmm, I wonder if part of the problem is not the database, but rather the the processing on the web server side? Do you do much manipulation of the results from the DB? Or is it simply a case of putting them into a template?

(It's probably also worth noting that PythonAnywhere may in general be slower than your local machine, given that locally you have access to 100% of a machine that costs, say, $1200 -- which means $50/month assuming that it lasts for two years, plus costs for maintenance, electricity, bandwidth, cooling and so on. But a 3x slowdown on simple DB queries does sound suspicious even given that.)

Hey Giles,

Sorry I didn't get back to you. OK, no problem. For now I will leave it like that, and in a few weeks I will do some performance tests to find ways to optimize the algorithm. I'm not an expert programmer, so I'm sure there is a lot of room for improvement.

Thx!

Glad to help :-) BTW the first thing I'd always do when trying to optimize database stuff is to check that you have appropriate indexes set up. Unfortunately the definition of "appropriate" in this context is a little bit subjective, but if you google around you'll probably find useful advice.

Thanks! I'll look into that. Many things to learn and much room for improvement :)