no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cybiko:codetiming [2009/11/27 17:54] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Coding Timing====== | ||
+ | |||
+ | =====Like this?===== | ||
+ | |||
+ | Trying some writing to the MP3 smartcard. The following code took | ||
+ | 10secs to execute. Not hugely fast ... can you see if my rusty old C | ||
+ | code is in any way optimal? | ||
+ | <code c> | ||
+ | Time_get_RTC( &time ); | ||
+ | TRACE(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | ptr_file_output = (struct FileOutput*) malloc( sizeof *ptr_file_output ); | ||
+ | |||
+ | FileOutput_ctor_Ex( ptr_file_output, | ||
+ | |||
+ | for( index = 0; index < 30; index++ ) | ||
+ | { | ||
+ | memset( k1, index, 1024); | ||
+ | FileOutput_write( ptr_file_output, | ||
+ | } | ||
+ | |||
+ | FileOutput_dtor( ptr_file_output, | ||
+ | TRACE(" | ||
+ | Time_get_RTC( &time ); | ||
+ | TRACE(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | =====This is better===== | ||
+ | |||
+ | Well strictly speaking it didn't take 10sec to write to the device. | ||
+ | You included the memory allocation, the file pointer constructor / destructor and a couple of debugs to the console in that 10sec. | ||
+ | |||
+ | Ok these things shouldn' | ||
+ | |||
+ | You might consider the lightweight call clock() which returns a long in millisecond and use. | ||
+ | <code c> | ||
+ | long end, start; | ||
+ | start = clock(); | ||
+ | | ||
+ | end = clock(); | ||
+ | | ||
+ | </ | ||
+ | The reason you would code it that way rather than: | ||
+ | <code c> | ||
+ | | ||
+ | </ | ||
+ | Is because you can't be sure at what point the clock() function is evaluated and in doing timing measurement you don't want to measure the time of the debug code itself. | ||
+ | |||
+ | Of course start would need to be reset for the next measurement. | ||
+ | |||
+ | You can setup some macro' | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | So your code might now look like this: | ||
+ | <code c> | ||
+ | | ||
+ | |||
+ | | ||
+ | ptr_file_output = (struct FileOutput*) malloc( sizeof *ptr_file_output ); | ||
+ | | ||
+ | |||
+ | | ||
+ | FileOutput_ctor_Ex( ptr_file_output, | ||
+ | | ||
+ | |||
+ | | ||
+ | for( index = 0; index < 30; index++ ) | ||
+ | { | ||
+ | memset( k1, index, 1024); | ||
+ | FileOutput_write( ptr_file_output, | ||
+ | } | ||
+ | | ||
+ | |||
+ | | ||
+ | FileOutput_dtor( ptr_file_output, | ||
+ | | ||
+ | </ | ||
+ | |||
+ | These macro' | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | Just some thought. :) | ||