Michael Chuah / Mbed 2 deprecated Benchmark

Dependencies:   mbed

Fork of benchmark by Igor Martinovski

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //https://developer.mbed.org/users/igor_m/code/benchmark/
00004 /* This program determines the time it takes to perform floating point
00005     and integer operations.
00006     To determine the time it takes, a Timer is used to measure the time
00007     it takes to complete a large amount of iterations. The time for a single
00008     operation can then be determined.
00009 
00010     To increase accuracy of the results, an empty for loop is timed to determine
00011     the loop overhead and the overhead is subtracted from the time it takes to
00012     complete the operation loop.
00013 */
00014 
00015 #define ITERATIONS 1000000    // Number of calculations.
00016 #define CLOCK 64              // Clock freqency in MHz
00017 Timer timer;                       // Timer..
00018 
00019 Serial pc(USBTX, USBRX);
00020 float number_of_cycles, single_operation_time;
00021 //volatile int a, b, c;              // Int operands and result. Must be volatile!
00022 volatile float a, b, c;            // Float operands and result. Must be volatile!
00023 //volatile double a, b, c;            // Float operands and result. Must be volatile!
00024 
00025 int main() {
00026 
00027     // Apparently I'm not getting full speed!? 
00028     // https://developer.mbed.org/forum/mbed/topic/229/?page=1
00029     // https://developer.mbed.org/questions/55557/CPU-clock-of-upto-100MHz/  
00030     printf("SystemCoreClock = %d MHz\r\n", SystemCoreClock/1000000);
00031     // Need external crystal to make it run faster?
00032     // https://developer.mbed.org/users/dreschpe/code/ST_401_84MHZ/
00033     // https://developer.mbed.org/users/oliverb/notebook/crystal-oscillator-notes/
00034 
00035     unsigned int i, for_time, total_time, operation_time;
00036     a=2.3;
00037     b=5.33;
00038 
00039     timer.reset();      // Reset timer
00040     timer.start();      // Start timer
00041     pc.printf("Operations in progress.. May take some time.\r\n");
00042     /* Determine loop overhead */
00043     for (i=0; i<ITERATIONS; i++){}
00044     for_time=timer.read_us();
00045     timer.stop();
00046 
00047     /* Determine the total loop time */
00048     timer.reset();
00049     timer.start();
00050     
00051     /* The operation takes place in the body of
00052     this for loop. */
00053     for (i=0; i<ITERATIONS; i++){
00054         
00055 //        a = b;
00056 //        c = a+b;
00057 //        c = a*b;
00058 //        c = a/b;
00059 //        a = sqrt(b);
00060 //        a = log(b);
00061 //        a = tanh(b);
00062 //        a = (10*pow(b,3) + 105*b)/(pow(b,4) + 45*pow(b,2) + 105);
00063         a = (10*b*b*b + 105*b)/(b*b*b*b + 45* + 105);
00064     }
00065     
00066     total_time=timer.read_us();
00067 
00068     operation_time = total_time-for_time;   // Calculate the time it took for the number of operations
00069 
00070     single_operation_time=float(operation_time)/float(ITERATIONS);
00071     number_of_cycles = single_operation_time*CLOCK;
00072 
00073     pc.printf("for overhead: \t\t%dus \r\n", for_time);
00074     pc.printf("total time: \t\t%dus \r\n", total_time);
00075     pc.printf("%d calculations took:\t%dus \r\n", ITERATIONS, operation_time);
00076     pc.printf("single operation took: \t\t%fus \r\n", single_operation_time);
00077     pc.printf("single operation took: \t\t%.3f cycles \r\n", number_of_cycles);
00078 }