I am trying to compare the speed of two pixel plotting functions, but I cant seem to get the speeds to print out:


Code:

int main(void) {
   int t1,t2,t3,t4;
   int i,j;
   
   t1 = RTC_GetTicks();
   for(i=0;i<1000;i++)
   {   
      plot(0,0,COLOR_WHITE);
   }
   t2 = RTC_GetTicks();
   
   t3 = RTC_GetTicks();
   for(j=0;j<1000;j++)
   {
      plot_asm(1,1,COLOR_WHITE);
   }
   t4 = RTC_GetTicks();
   
   t1 = (t2-t1)/128;
   t3 = (t4-t3)/128;
   
   unsigned char time1[sizeof(t1)+3];
   char time2[sizeof(t3)+3];
   
   time1[0] = 'X';
   time1[1] = 'X';
   time2[0] = 'X';
   time2[1] = 'X';
   time1[2] = 'X';
   time2[2] = 'X';
   
   memcpy(&time1[3],&t1,sizeof(t1));
   memcpy(&time2[3],&t3,sizeof(t3));
   
   unsigned char * time1loc = &time1[0];
   char * time2loc = &time2[0];
   
   PrintXY(1, 1, "XXResults:", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
   int x = 0;
   int y = 50;
   PrintMini( &x, &y, time1loc, 0x02, 350, 0, 0, TEXT_COLOR_BLACK, TEXT_COLOR_WHITE, 1, 0);
   PrintXY(1, 4, time2loc, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
   
   while(!keydownlast(KEY_PRGM_MENU))
   {
      Bdisp_PutDisp_DD();
      keyupdate();
   }
   
   return 0;
}


PrintMini prints out three Xs, and PrintXY displays one X, but neither have any numbers following them. Am I using memcpy correctly here?
t1 and t3 are numbers, not strings. You'll want to convert them to strings first using itoa and then copy it or concat or w/e. In Minesweeper I do something like:

Code:
      unsigned char out[3];
      itoa(numMines-numFlags, out);
      DefineStatusMessage(concat("  Minesweeper!    Mines: ", (char*)out), 1, 0, 0);
Your attempt won't work at all, because the machine representation of numbers is not a string.

itoa is reasonable I guess, but is rather less portable than sprintf (it's not part of any C standard).

The usual way to handle this is with some variant on sprintf:
Code:
#include <stdio.h>

void display_uint(int x, int y, unsigned value) {
    char buf[13];
    sprintf(buf + 2, "%u", value);
    PrintXY(x, y, buf, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
}
The necessary size of buf is derived from the valid range of unsigned int (32 bits): ⌈log10(232-1)⌉ plus two ignored bytes for PrintXY and one for a null terminator.
While I do see your argument for using sprintf you do have to remember that the program expands in size as sprintf must now be linked into your program.
I think sprintf should only be used if you are doing lots of diverse string manipulation.
However if you just want to display a number use itoa.
I understand that the size savings is not that much as we have 16mb of memory and are saving around 2kb.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement