Jovan Ivković / Mbed 2 deprecated Benchmark_Math_OP

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //********************************************************
00002 //**  Nucleo-144 Stm32F746 and Stm32F767 benchmark  ******
00003 //**        DP, SP and Int Arithemtic ops.          ******
00004 //**  modified from ddown post at Arduino for STM32 ******
00005 //**  github.com/ddrown/benchmark.c (Arduino IDE)   ******
00006 //**                     by                         ******              
00007 //**          Jovan Ivković - 2016.                 ******
00008 //**         JovanEps (jovan.eps@gmail.com).        ******
00009 //********************************************************
00010 #include "mbed.h"
00011 DigitalOut myled(LED1);
00012 Serial pc(USBTX, USBRX);
00013 Timer timer;
00014 
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <math.h>
00019 /* the following is optional depending on the timing function used */
00020 #include <time.h>
00021 
00022 
00023 #define REDO_COMPUTATIONS 10
00024 struct results {
00025   uint32_t doubletime;
00026   uint32_t floattime;
00027   uint32_t inttime;
00028 };
00029 
00030 #define MAX_LOOPS 512
00031 double MyDoubles[MAX_LOOPS];
00032 double a_d = 12345.67, b_d = 54321.11;
00033 float MyFloats[MAX_LOOPS];
00034 float a_f = 67890.12, b_f = 8756451.17;
00035 int Myints[MAX_LOOPS];
00036 int a_i = 581674411, b_i = 18714;
00037 
00038 //****************************************
00039 uint32_t micros ()
00040 {
00041 //****************************************
00042     // uint32_t usec = timer.read_us();
00043     //return usec;
00044     return timer.read_us();
00045 }
00046 
00047 //****************************************
00048 void math_add (struct results *r) {
00049 //****************************************
00050   uint32_t t, c, l;
00051   
00052   t = micros ();
00053   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00054   {
00055     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00056     { 
00057       MyDoubles [ l ] = double ( a_d + b_d * double ( l ) );
00058     }
00059   }
00060   r->doubletime = micros () - t;
00061 
00062   t = micros ();
00063   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00064   {
00065     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00066     { 
00067       MyFloats [ l ] = float ( a_f + b_f * float ( l ) );
00068     }
00069   }
00070   r->floattime = micros () - t;
00071 
00072   t = micros ();
00073   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00074   {
00075     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00076     { 
00077       Myints [ l ] =  a_i + b_i * l;
00078     }
00079   }
00080   r->inttime = micros () - t;
00081 }
00082 
00083 //****************************************
00084 void math_sub (struct results *r) {
00085 //****************************************  
00086   uint32_t t, c, l;
00087   
00088   t = micros ();
00089   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00090   {
00091     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00092     { 
00093       MyDoubles [ l ] = double ( a_d - b_d * double ( l ) );
00094     }
00095   }
00096   r->doubletime = micros () - t;
00097 
00098   t = micros ();
00099   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00100   {
00101     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00102     { 
00103       MyFloats [ l ] = float ( a_f - b_f * float ( l ) );
00104     }
00105   }
00106   r->floattime = micros () - t;
00107 
00108   t = micros ();
00109   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00110   {
00111     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00112     { 
00113       Myints [ l ] =  a_i - b_i * l;
00114     }
00115   }
00116   r->inttime = micros () - t;
00117 }
00118 
00119 //****************************************
00120 void math_mul (struct results *r) {
00121 //****************************************
00122   uint32_t t, c, l;
00123     
00124   t = micros ();
00125   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00126   {
00127     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00128     { 
00129       MyDoubles [ l ] = double ( a_d * b_d * double ( l ) );
00130     }
00131   }
00132   r->doubletime = micros () - t;
00133 
00134   t = micros ();
00135   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00136   {
00137     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00138     { 
00139       MyFloats [ l ] = float ( a_f * b_f * float ( l ) );
00140     }
00141   }
00142   r->floattime = micros () - t;
00143 
00144   t = micros ();
00145   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00146   {
00147     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00148     { 
00149       Myints [ l ] =  a_i * b_i * l;
00150     }
00151   }
00152   r->inttime = micros () - t;
00153 }
00154 
00155 //****************************************
00156 void math_div (struct results *r) {
00157 //****************************************
00158   uint32_t t, c, l;
00159 
00160   t = micros ();
00161   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00162   {
00163     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00164     { 
00165       MyDoubles [ l ] = double ( a_d / b_d * double ( l ) );
00166     }
00167   }
00168   r->doubletime = micros () - t;
00169 
00170   t = micros ();
00171   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00172   {
00173     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00174     { 
00175       MyFloats [ l ] = float ( a_f / b_f * float ( l ) );
00176     }
00177   }
00178   r->floattime = micros () - t;
00179 
00180   t = micros ();
00181   for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
00182   {
00183     for ( l = 0 ; l < MAX_LOOPS ; l ++ )
00184     { 
00185       Myints [ l ] =  a_i / b_i * l;
00186     }
00187   }
00188   r->inttime = micros () - t;
00189 }
00190 
00191 //****************************************
00192 void bench_loop() {
00193 //****************************************
00194 
00195   struct results add_ops, sub_ops, mul_ops, div_ops;
00196 
00197   math_add(&add_ops);
00198   math_sub(&sub_ops);
00199   math_mul(&mul_ops);
00200   math_div(&div_ops);
00201 
00202   pc.printf("\n\n");
00203   pc.printf("\n     FUNCTION            DOUBLE         SINGLE         INT");
00204   
00205   pc.printf("\n Time - ADD (us/512) :\t ");
00206   pc.printf( "%0.1f", ( float ) add_ops.doubletime / REDO_COMPUTATIONS );
00207   pc.printf("\t\t");
00208   pc.printf( "%0.1f", ( float ) add_ops.floattime / REDO_COMPUTATIONS );
00209   pc.printf("\t\t");
00210   pc.printf("%0.1f", ( float ) add_ops.inttime / REDO_COMPUTATIONS );
00211   pc.printf("\n");
00212   
00213   pc.printf("\n Time - SUB (us/512) :\t ");
00214   pc.printf( "%0.1f", ( float ) sub_ops.doubletime / REDO_COMPUTATIONS );
00215   pc.printf("\t\t");
00216   pc.printf( "%0.1f", ( float ) sub_ops.floattime / REDO_COMPUTATIONS );
00217   pc.printf("\t\t");
00218   pc.printf( "%0.1f", ( float ) sub_ops.inttime / REDO_COMPUTATIONS );
00219   pc.printf("\n");
00220 
00221   pc.printf("\n Time - MUL (us/512) :\t ");
00222   pc.printf( "%0.1f", ( float ) mul_ops.doubletime / REDO_COMPUTATIONS );
00223   pc.printf("\t\t");
00224   pc.printf( "%0.1f", ( float ) mul_ops.floattime / REDO_COMPUTATIONS );
00225   pc.printf("\t\t");
00226   pc.printf( "%0.1f", ( float ) mul_ops.inttime / REDO_COMPUTATIONS );
00227   pc.printf("\n");
00228 
00229   pc.printf("\n Time - DIV (us/512) :\t ");
00230   pc.printf( "%0.1f", ( float ) div_ops.doubletime / REDO_COMPUTATIONS );
00231   pc.printf("\t\t");
00232   pc.printf( "%0.1f", ( float ) div_ops.floattime / REDO_COMPUTATIONS );
00233   pc.printf("\t\t");
00234   pc.printf( "%0.1f", ( float ) div_ops.inttime / REDO_COMPUTATIONS );
00235   pc.printf("\n");
00236   
00237   wait(1);
00238 }
00239 
00240 //*********************************
00241 //**         MAIN block          **
00242 //*********************************
00243 int main()
00244 {
00245     pc.baud(57600);
00246     
00247     pc.printf("\n My Benchamrk ...");
00248     pc.printf("Beginningbenchmark at ");
00249     pc.printf("default 216 MHz ...\n");
00250     pc.printf("\n\n");   
00251            
00252     while(1)
00253     {
00254         myled=1;
00255         timer.start();
00256         
00257         bench_loop(); //Call of banch method
00258         
00259         pc.printf(" kraj \n");
00260         myled=0;
00261         timer.stop();
00262         
00263     }
00264 }