Igor Martinovski / Mbed 2 deprecated benchmark

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /* This program determines the time it takes to perform floating point
00003     and integer operations.
00004     To determine the time it takes, a Timer is used to measure the time
00005     it takes to complete a large amount of iterations. The time for a single
00006     operation can then be determined.
00007 
00008     To increase accuracy of the results, an empty for loop is timed to determine
00009     the loop overhead and the overhead is subtracted from the time it takes to
00010     complete the operation loop.
00011 
00012     */
00013 #define ITERATIONS 1000000    // Number of calculations.
00014 #define CLOCK 96              // Clock freqency in MHz
00015 Timer timer;                       // Timer..
00016 
00017 Serial pc(USBTX, USBRX);
00018 float number_of_cycles, single_operation_time;
00019 volatile float a, b,c;            // Float operands and result. Must be volatile!
00020 //volatile int a, b,c;              // Int operands and result. Must be volatile!
00021 
00022 int main() {
00023 
00024     unsigned int i, for_time, total_time, operation_time;
00025     a=2.3;
00026     b=5.33;
00027 
00028     timer.reset();      // Reset timer
00029     timer.start();      // Start timer
00030     pc.printf("Operations in progress.. May take some time.\n\n");
00031     /* Determine loop overhead */
00032     for (i=0; i<ITERATIONS; i++);
00033     for_time=timer.read_us();
00034     timer.stop();
00035 
00036     /* Determine the total loop time */
00037     timer.reset();
00038     timer.start();
00039     
00040     /* The operation takes place in the body of
00041     this for loop. */
00042     
00043     for (i=0; i<ITERATIONS; i++) {
00044         
00045         c=a+b;
00046         //c=sin(a);
00047         //c=sqrt(a);
00048 
00049     }
00050     total_time=timer.read_us();
00051 
00052     operation_time = total_time-for_time;   // Calculate the time it took for the number of operations
00053 
00054     single_operation_time=float(operation_time)/float(ITERATIONS);
00055     number_of_cycles = single_operation_time*CLOCK;
00056 
00057     pc.printf("for overhead: \t\t%dus \n", for_time);
00058     pc.printf("total time: \t\t%dus \n\n", total_time);
00059     pc.printf("%d calculations took:\t%dus \n", ITERATIONS, operation_time);
00060     pc.printf("single operation took: \t\t%fus\n", single_operation_time);
00061     pc.printf("single operation took: \t\t%.3f cycles\n", number_of_cycles);
00062 }