Sneaky slowness

I noticed, since I added similar song, that gmpc got slowish in the metadata browser.

So today I decided to profile it, so I installed sysprof. After fixing a bug in ubuntu package (see here) I ran it.

For me totally unexpected, it was saving the metadata db that took so much time. Everytime a change was made, it took around 6 seconds to save it.

So time to optimise, First thing was to only save it on close. Not on every change. But even on close, 6 seconds is way to long.

First thing I did was remove all calls to fprintf and replace them by fput and putc. This reduced the time to 3.5 second.  Still to long.

After fiddling around,  I saw the bug, this was part of the code:

1
for (i=0; i < strlen(string);i++) {..

I always thought (wrongly offcourse) that gcc was smart enough not to run strlen on each itteration.

After changing it to

1
2
3
int length = strlen(string);
 
for(i=0;i < length;i++){..

The total time saving the 1.7mb database is  0.17 seconds. Now this is acceptable on close.

One tiny change, speed difference of a life time.

Q

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>