pawel wzietek / Mbed 2 deprecated benchmarks for F303RE

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //simple benchmark, see main for results  
00004 
00005 DigitalOut led(LED1);
00006 
00007 
00008 AnalogIn   Ain(A0);       
00009 
00010 
00011 Serial pc(USBTX, USBRX); // tx, rx
00012 
00013 Timer Tim, Timc;
00014 int dt;
00015 float dtavg;
00016 
00017 
00018 #define Nsamp 2000
00019 unsigned short samples[Nsamp];   //analogin sample table 
00020 
00021 volatile float x,s;  //volatile to prevent compiler from optimizing loops
00022 volatile float y;
00023 
00024 volatile double dx, ds;
00025 volatile double dy;  
00026 
00027 volatile long ix,iy,is;
00028 
00029 //************************
00030 inline void test_acq()
00031 {
00032     int N=Nsamp;
00033       register int i;
00034          Tim.reset(); Tim.start();
00035       for(i=0; i<N; i++) {
00036         samples[i] = Ain.read_u16();
00037     }
00038     Tim.stop();
00039     dt=Tim.read_us(); 
00040     dtavg=dt/(float) Nsamp;
00041     pc.printf(" input of %d values: total %d, avg: %f \r\n",N, dt, dtavg);
00042     
00043 }
00044 
00045 
00046 
00047 inline void testmullong()
00048 {
00049 int N=100000;
00050 
00051 x=Timc.read_ms(); y=Timc.read_ms();
00052 Tim.reset(); Tim.start();
00053 Timc.start();
00054 
00055 for(int i=0; i<N; i++) {
00056        is=is+ix*iy;
00057     }
00058     Tim.stop();
00059     dt=Tim.read_us(); 
00060     dtavg=dt/(float)N;
00061     pc.printf(" add-mult of %d longs: total %d, avg: %f \r\n", N, dt, dtavg);
00062    
00063 }
00064 
00065 
00066 inline void testmulfloat()
00067 {
00068 int N=100000;
00069 
00070 x=Timc; y=Timc;
00071 Tim.reset(); Tim.start();
00072 
00073 
00074 for(int i=0; i<N; i++) {
00075        s=s+x*y;
00076     }
00077     Tim.stop();
00078     dt=Tim.read_us(); 
00079     dtavg=dt/(float)N;
00080     pc.printf(" add-mult of %d floats: total %d, avg: %f \r\n", N, dt, dtavg);
00081    
00082 }
00083 
00084 
00085 inline void testdivfloat()
00086 {
00087 int N=100000;
00088 
00089 x=Timc; y=Timc;
00090 Tim.reset(); Tim.start();
00091 
00092 
00093 for(int i=0; i<N; i++) {
00094        s=s+x/y;
00095     }
00096     Tim.stop();
00097     dt=Tim.read_us(); 
00098     dtavg=dt/(float)N;
00099     pc.printf(" add-div of %d floats: total %d, avg: %f \r\n", N, dt, dtavg);
00100    
00101 }
00102 
00103 
00104 inline void testsin()
00105 {
00106 int N=10000;
00107 
00108 x=Timc; 
00109 Tim.reset(); Tim.start();
00110 
00111 
00112 for(int i=0; i<N; i++) {
00113        s=s+sin(x);
00114     }
00115     Tim.stop();
00116     dt=Tim.read_us(); 
00117     dtavg=dt/(float)N;
00118     pc.printf(" add-sin of %d floats: total %d, avg: %f \r\n", N, dt, dtavg);
00119    
00120 }
00121 
00122 
00123 inline void testmuldouble()
00124 {
00125 int N=100000;
00126 
00127 dx=Timc; dy=Timc;
00128 Tim.reset(); Tim.start();
00129 
00130 for(int i=0; i<N; i++) {
00131        ds=ds+dx*dy;
00132     }
00133     Tim.stop();
00134     dt=Tim.read_us(); 
00135     dtavg=dt/(float)N;
00136     pc.printf(" add-mult of %d doubles: total %d, avg: %f \r\n", N, dt, dtavg);
00137    
00138 }
00139 
00140 
00141 inline void testdivdouble()
00142 {
00143 int N=100000;
00144 
00145 dx=Timc; dy=Timc;
00146 Tim.reset(); Tim.start();
00147 
00148 for(int i=0; i<N; i++) {
00149        ds=ds+dx/dy;
00150     }
00151     Tim.stop();
00152     dt=Tim.read_us(); 
00153     dtavg=dt/(float)N;
00154     pc.printf("add-div of %d doubles: total %d, avg: %f \r\n", N, dt, dtavg);
00155    
00156 }
00157 
00158 
00159 
00160 //**********************
00161 
00162 int main() {
00163 
00164 
00165   //pc.baud(115200);  //set terminal to 115200 baud
00166   
00167     pc.printf("start\n");
00168     
00169     Tim.reset();
00170     led=0;
00171 
00172     while(1) 
00173     {
00174         led = 1-led; 
00175            pc.printf("\n\n ");
00176       test_acq();  //KL25z: 32.6us per loop;  F303RE: 8.1us per loop
00177       testmullong();  //KL25z: 0.25  F303RE: 0.132
00178       testmulfloat();  //KL25z: 2.61  F303RE: 0.187
00179       testdivfloat(); // KL25z: 3.68     F303RE: 0.35   
00180       testsin();      //KL25z: 13.6      F303RE: 1.57
00181       testmuldouble();  //KL25z: 8.44  F303RE: 6.84
00182       testdivdouble();  //KL25z: 13.7 F303RE: 17.5
00183       
00184       wait(1); 
00185     }
00186     
00187 }