sprintf speed nightmare

SGDK only sub forum

Moderator: Stef

Post Reply
matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

sprintf speed nightmare

Post by matteus » Wed Sep 14, 2016 8:24 am

Code: Select all

sprintf(Verse1.charLine1, "%s %s", Lyrics.charLine1Part1[randomNumberGenerator(10)], Lyrics.charLine1Part2[randomNumberGenerator(10)]);
            sprintf(Verse1.charLine2, "%s %s", Lyrics.charLine2Part1[randomNumberGenerator(10)], Lyrics.charLine2Part2[randomNumberGenerator(10)]);
            sprintf(Verse1.charLine3, "%s", Lyrics.charLine3[randomNumberGenerator(10)]);
            sprintf(Verse1.charLine4, "%s", Lyrics.charLine4[randomNumberGenerator(10)]);
            sprintf(Verse2.charLine1, "%s %s", Lyrics.charLine1Part1[randomNumberGenerator(10)], Lyrics.charLine1Part2[randomNumberGenerator(10)]);
            sprintf(Verse2.charLine2, "%s %s", Lyrics.charLine2Part1[randomNumberGenerator(10)], Lyrics.charLine2Part2[randomNumberGenerator(10)]);
            sprintf(Verse2.charLine3, "%s", Lyrics.charLine3[randomNumberGenerator(10)]);
            sprintf(Verse2.charLine4, "%s", Lyrics.charLine4[randomNumberGenerator(10)]);
            sprintf(Verse3.charLine1, "%s %s", Lyrics.charLine1Part1[randomNumberGenerator(10)], Lyrics.charLine1Part2[randomNumberGenerator(10)]);
            sprintf(Verse3.charLine2, "%s %s", Lyrics.charLine2Part1[randomNumberGenerator(10)], Lyrics.charLine2Part2[randomNumberGenerator(10)]);
            sprintf(Verse3.charLine3, "%s", Lyrics.charLine3[randomNumberGenerator(10)]);
I have the following code that seems to take an age to run! I'm guessing it's something to do with the memory speed?

Stef
Very interested
Posts: 2976
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: sprintf speed nightmare

Post by Stef » Wed Sep 14, 2016 1:45 pm

It's just that sprintf is a *very slow* method (which use an internal interpreter) specially on a system like the genesis ;)

cero
Very interested
Posts: 259
Joined: Mon Nov 30, 2015 1:55 pm

Re: sprintf speed nightmare

Post by cero » Wed Sep 14, 2016 5:40 pm

That would also be slow on a PC, but the 4GHz speed is excellent at hiding bad coding ;)

Code: Select all

sprintf(Verse3.charLine3, "%s", Lyrics.charLine3[randomNumberGenerator(10)]);
Printing just one string, with no modifications of any kind, is quite wasteful this way. You should use strcpy in those cases.

edit: Modern GCC optimizes it automatically. Not sure about the old gcc 3.

Stef
Very interested
Posts: 2976
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: sprintf speed nightmare

Post by Stef » Wed Sep 14, 2016 7:28 pm

Almost certain GCC 3 does not optimize it ;)

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: sprintf speed nightmare

Post by matteus » Thu Sep 15, 2016 8:52 am

Code: Select all

            strcpy(Verse1.charLine1Part1, Lyrics.charLine1Part1[randomNumberGenerator(10)]);
            strcpy(Verse1.charLine2Part1, Lyrics.charLine1Part1[randomNumberGenerator(10)]);
            SYS_disableInts();
            VDP_drawText(Verse1.charLine1Part1, SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+7);
            VDP_drawText(Lyrics.charLine1Part2[randomNumberGenerator(10)], SONG_TEXT_START_COLUMN+strlen(Verse1.charLine1Part1)+1, SONG_TEXT_START_ROW+7);
            VDP_drawText(Verse1.charLine2Part1, SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+8);
            VDP_drawText(Lyrics.charLine1Part2[randomNumberGenerator(10)], SONG_TEXT_START_COLUMN+strlen(Verse1.charLine2Part1)+1, SONG_TEXT_START_ROW+8);
            VDP_drawText(Lyrics.charLine3[randomNumberGenerator(10)], SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+9);
            VDP_drawText(Lyrics.charLine4[randomNumberGenerator(10)], SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+10);
            SYS_enableInts();
So is this any better?

cero
Very interested
Posts: 259
Joined: Mon Nov 30, 2015 1:55 pm

Re: sprintf speed nightmare

Post by cero » Thu Sep 15, 2016 9:46 am

Yes, that's much faster.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: sprintf speed nightmare

Post by matteus » Thu Sep 15, 2016 8:12 pm

Noticeable too! :) Thank you so much

KanedaFr
Administrateur
Posts: 1127
Joined: Tue Aug 29, 2006 10:56 am
Contact:

Re: sprintf speed nightmare

Post by KanedaFr » Sun Sep 18, 2016 10:51 am

Is there a reason for you to use

Code: Select all

strcpy(Verse1.charLine1Part1, Lyrics.charLine1Part1[randomNumberGenerator(10)]);
 ...
 VDP_drawText(Verse1.charLine1Part1, SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+7);
and not

Code: Select all

 VDP_drawText( Lyrics.charLine1Part1[randomNumberGenerator(10)], SONG_TEXT_START_COLUMN, SONG_TEXT_START_ROW+7);
?

From your code snipped, I don't see the need to use strcpy...
if it's for you to keep the string for another use, you should perhaps better save the result of randomNumberGenerator(10) than to store a string
no ?

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: sprintf speed nightmare

Post by matteus » Mon Sep 19, 2016 5:02 pm

You'll notice further down I used a strlen on Verse1.charLine1Part1, I have to store this in char to get it's length later on in the code :)

Post Reply