This is a simple program that can be used to determine how long a single operation takes. The program outputs the results in microseconds and number of clock cycles.

Dependencies:   mbed

Committer:
igor_m
Date:
Mon Dec 06 22:37:52 2010 +0000
Revision:
0:6d89d8c13042

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igor_m 0:6d89d8c13042 1 #include "mbed.h"
igor_m 0:6d89d8c13042 2 /* This program determines the time it takes to perform floating point
igor_m 0:6d89d8c13042 3 and integer operations.
igor_m 0:6d89d8c13042 4 To determine the time it takes, a Timer is used to measure the time
igor_m 0:6d89d8c13042 5 it takes to complete a large amount of iterations. The time for a single
igor_m 0:6d89d8c13042 6 operation can then be determined.
igor_m 0:6d89d8c13042 7
igor_m 0:6d89d8c13042 8 To increase accuracy of the results, an empty for loop is timed to determine
igor_m 0:6d89d8c13042 9 the loop overhead and the overhead is subtracted from the time it takes to
igor_m 0:6d89d8c13042 10 complete the operation loop.
igor_m 0:6d89d8c13042 11
igor_m 0:6d89d8c13042 12 */
igor_m 0:6d89d8c13042 13 #define ITERATIONS 1000000 // Number of calculations.
igor_m 0:6d89d8c13042 14 #define CLOCK 96 // Clock freqency in MHz
igor_m 0:6d89d8c13042 15 Timer timer; // Timer..
igor_m 0:6d89d8c13042 16
igor_m 0:6d89d8c13042 17 Serial pc(USBTX, USBRX);
igor_m 0:6d89d8c13042 18 float number_of_cycles, single_operation_time;
igor_m 0:6d89d8c13042 19 volatile float a, b,c; // Float operands and result. Must be volatile!
igor_m 0:6d89d8c13042 20 //volatile int a, b,c; // Int operands and result. Must be volatile!
igor_m 0:6d89d8c13042 21
igor_m 0:6d89d8c13042 22 int main() {
igor_m 0:6d89d8c13042 23
igor_m 0:6d89d8c13042 24 unsigned int i, for_time, total_time, operation_time;
igor_m 0:6d89d8c13042 25 a=2.3;
igor_m 0:6d89d8c13042 26 b=5.33;
igor_m 0:6d89d8c13042 27
igor_m 0:6d89d8c13042 28 timer.reset(); // Reset timer
igor_m 0:6d89d8c13042 29 timer.start(); // Start timer
igor_m 0:6d89d8c13042 30 pc.printf("Operations in progress.. May take some time.\n\n");
igor_m 0:6d89d8c13042 31 /* Determine loop overhead */
igor_m 0:6d89d8c13042 32 for (i=0; i<ITERATIONS; i++);
igor_m 0:6d89d8c13042 33 for_time=timer.read_us();
igor_m 0:6d89d8c13042 34 timer.stop();
igor_m 0:6d89d8c13042 35
igor_m 0:6d89d8c13042 36 /* Determine the total loop time */
igor_m 0:6d89d8c13042 37 timer.reset();
igor_m 0:6d89d8c13042 38 timer.start();
igor_m 0:6d89d8c13042 39
igor_m 0:6d89d8c13042 40 /* The operation takes place in the body of
igor_m 0:6d89d8c13042 41 this for loop. */
igor_m 0:6d89d8c13042 42
igor_m 0:6d89d8c13042 43 for (i=0; i<ITERATIONS; i++) {
igor_m 0:6d89d8c13042 44
igor_m 0:6d89d8c13042 45 c=a+b;
igor_m 0:6d89d8c13042 46 //c=sin(a);
igor_m 0:6d89d8c13042 47 //c=sqrt(a);
igor_m 0:6d89d8c13042 48
igor_m 0:6d89d8c13042 49 }
igor_m 0:6d89d8c13042 50 total_time=timer.read_us();
igor_m 0:6d89d8c13042 51
igor_m 0:6d89d8c13042 52 operation_time = total_time-for_time; // Calculate the time it took for the number of operations
igor_m 0:6d89d8c13042 53
igor_m 0:6d89d8c13042 54 single_operation_time=float(operation_time)/float(ITERATIONS);
igor_m 0:6d89d8c13042 55 number_of_cycles = single_operation_time*CLOCK;
igor_m 0:6d89d8c13042 56
igor_m 0:6d89d8c13042 57 pc.printf("for overhead: \t\t%dus \n", for_time);
igor_m 0:6d89d8c13042 58 pc.printf("total time: \t\t%dus \n\n", total_time);
igor_m 0:6d89d8c13042 59 pc.printf("%d calculations took:\t%dus \n", ITERATIONS, operation_time);
igor_m 0:6d89d8c13042 60 pc.printf("single operation took: \t\t%fus\n", single_operation_time);
igor_m 0:6d89d8c13042 61 pc.printf("single operation took: \t\t%.3f cycles\n", number_of_cycles);
igor_m 0:6d89d8c13042 62 }