Skip to content

Instantly share code, notes, and snippets.

@ddrown
Last active April 18, 2016 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddrown/3c6de60a6160b7823250f34ec350a9ca to your computer and use it in GitHub Desktop.
Save ddrown/3c6de60a6160b7823250f34ec350a9ca to your computer and use it in GitHub Desktop.
#define REDO_COMPUTATIONS 100
struct results {
uint32_t doubletime;
uint32_t floattime;
uint32_t inttime;
};
#define MAX_LOOPS 512
double MyDoubles[MAX_LOOPS];
double a_d = 12345.67, b_d = 54321.11;
float MyFloats[MAX_LOOPS];
float a_f = 67890.12, b_f = 8756451.17;
int Myints[MAX_LOOPS];
int a_i = 5814411, b_i = 18714;
void math_add (struct results *r) {
uint32_t t, c;
int l;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyDoubles [ l ] = double ( a_d + b_d * double ( l ) );
}
}
r->doubletime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyFloats [ l ] = float ( a_f + b_f * float ( l ) );
}
}
r->floattime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
Myints [ l ] = a_i + b_i * l;
}
}
r->inttime = micros () - t;
}
void math_sub (struct results *r) {
uint32_t t, c;
int l;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyDoubles [ l ] = double ( a_d - b_d * double ( l ) );
}
}
r->doubletime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyFloats [ l ] = float ( a_f - b_f * float ( l ) );
}
}
r->floattime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
Myints [ l ] = a_i - b_i * l;
}
}
r->inttime = micros () - t;
}
void math_mul (struct results *r) {
uint32_t t, c;
int l;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyDoubles [ l ] = double ( a_d * b_d * double ( l ) );
}
}
r->doubletime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyFloats [ l ] = float ( a_f * b_f * float ( l ) );
}
}
r->floattime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
Myints [ l ] = a_i * b_i * l;
}
}
r->inttime = micros () - t;
}
void math_div (struct results *r) {
uint32_t t, c;
int l;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyDoubles [ l ] = double ( a_d / b_d * double ( l ) );
}
}
r->doubletime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
MyFloats [ l ] = float ( a_f / b_f * float ( l ) );
}
}
r->floattime = micros () - t;
t = micros ();
for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
{
for ( l = 0 ; l < MAX_LOOPS ; l ++ )
{
Myints [ l ] = a_i / b_i * l;
}
}
r->inttime = micros () - t;
}
#ifdef STM32F4
#define SERIAL_PORT Serial1
#else
#define SERIAL_PORT Serial
#endif
void setup() {
SERIAL_PORT.begin(115200);
}
void loop() {
struct results add_ops, sub_ops, mul_ops, div_ops;
math_add(&add_ops);
math_sub(&sub_ops);
math_mul(&mul_ops);
math_div(&div_ops);
SERIAL_PORT.print("run ");
SERIAL_PORT.print(REDO_COMPUTATIONS);
SERIAL_PORT.println(" times");
SERIAL_PORT.println("FUNCTION DOUBLE SINGLE INT");
SERIAL_PORT.print("Time - ADD (us/512) :\t ");
SERIAL_PORT.print( add_ops.doubletime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.print( add_ops.floattime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.println( add_ops.inttime / REDO_COMPUTATIONS );
SERIAL_PORT.print("Time - SUB (us/512) :\t ");
SERIAL_PORT.print( sub_ops.doubletime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.print( sub_ops.floattime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.println ( sub_ops.inttime / REDO_COMPUTATIONS );
SERIAL_PORT.print ("Time - MUL (us/512) :\t ");
SERIAL_PORT.print( mul_ops.doubletime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.print( mul_ops.floattime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.println ( mul_ops.inttime / REDO_COMPUTATIONS );
SERIAL_PORT.print ("Time - DIV (us/512) :\t ");
SERIAL_PORT.print( div_ops.doubletime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.print( div_ops.floattime / REDO_COMPUTATIONS );
SERIAL_PORT.print("\t\t");
SERIAL_PORT.println ( div_ops.inttime / REDO_COMPUTATIONS );
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment