Need a math sketch to compare Teensy 3.0 to Uno

I received my Teensy 3.0 last night and for kicks, I would like to like to run a sketch that requires heavy math and can report millis() time. Then, I will compare the results from the Teensy to the Uno results.

I plan to report the results on the thread for Teensy 3.0. I can handle reporting millis() but, I have no idea what kind of math would demonstrate processing speed.

Would anyone like to give me a sketch or suggest what kind of math formula I should use for a sketch?

Thanks, Mark

Something simple, like sines of angles from -2? to +2? ?

Edited to post corrected function:

I have made a small code to measure speed of functions.

unsigned int chrono_id;
#define CHRONO(iterations,function) \
{\
  chrono_id ++;\
\
  Serial.print("Chrono ");\
  Serial.print(chrono_id);\
  Serial.print(" - "#function" - "#iterations" calls...\n");\
\
  unsigned long time = micros();\
\
  for (unsigned long i = 0; i < iterations; i++)\
  {\
    function;\
  }\
\
  time = micros()-time;\
  Serial.print("Average (microseconds) : ");\
  Serial.print((float)time/(float)iterations);\
  Serial.print("\n\n");\
}

Example usage:

void setup()
{
  Serial.begin(9600);
  delay(1000);
  
  CHRONO(100000, random(10));
  CHRONO(100000, micros());
  CHRONO(100000,
  {
    sqrt(pow(2, 32));
    sin(cos(tan(123456789)));
  });
}

Example output:

Chrono 1 - random(10) - 100000 calls...
Average (microseconds) : 140.21

Chrono 2 - micros() - 100000 calls...
Average (microseconds) : 3.97

Chrono 3 - { sqrt(pow(2, 32)); sin(cos(tan(123456789))); } - 100000 calls...
Average (microseconds) : 0.00

More iterations = more accurate result.

Oh and I know, it’s ugly, it isn’t a good practice…but couldn’t find a way to make it in a normal function :smiley:

Thank you Awol and Guix!

@Guix, I downloaded the library you linked to but, I did not figure out how to put it to use. The code you posted is a little above my understanding at the moment. Thank you for sharing it though.

Here is what I wrote up that is much more simple but, it will do.

To see a bench mark, I wrote a code to compare the Teensy 3.0 to the Uno. It calculates sin() of 0 to 400.

Rather impressive difference!

Code:

[code]
float sinanswers[401];

unsigned long time1 = 0;
unsigned long time2 = 0;
void setup()
{

  Serial.begin(9600); // USB is always 12 Mbit/sec
  delay(1000);
  Serial.print("Here");
  delay(4000);
  time1 = micros();
  for (int i = 0; i < 400; i++)
  {
    sinanswers[i] = sin(i);

  }
  time2 = micros();

  unsigned long elapsed = time2 - time1;
  for (int i = 0; i < 400; i++)
  {
    Serial.println(sinanswers[i]);
    Serial.print(" ");
    Serial.println(i);
  }

  Serial.print("time elasped = ");
  Serial.print(elapsed);
  Serial.print(" micros");
}

void loop()
{

}

Results:

  //time elasped = 29721 micros = teensy
  //time elasped = 47436 micros = Uno

[/code]

47932.72 with Sainsmart's Arduino MEGA 2560 :(

cyclegadget: @Guix, I downloaded the library you linked to but, I did not figure out how to put it to use. The code you posted is a little above my understanding at the moment. Thank you for sharing it though.

If you want you can simply replace those 2 lines in my code:

sprintf(result, "Average (microseconds) : %10.2f\n\n", ((float)time/(float)iterations));\
Serial.print(result);\

by this:

Serial.print("Average (microseconds) : ");\
Serial.print((float)time/(float)iterations);\
Serial.print("\n\n");\

Running the same code more than one time is needed for accurate results, but your test already show the winner anyway :)

18mS - due to using internal USB vs using UART to send data out?

CrossRoads: 18mS - due to using internal USB vs using UART to send data out?

Unless I made a mistake, I did the timing right before the math and after. The serial was not used during the math test.

Okay I see that - then why the difference between these 2 8-bit processors?

CrossRoads: Okay I see that - then why the difference between these 2 8-bit processors?

Teensy 3.0 is 32bit and runs at a much high clock.

http://www.kickstarter.com/projects/paulstoffregen/teensy-30-32-bit-arm-cortex-m4-usable-in-arduino-a

That would do it then.

This code will compile with Uno board selected but will not with the Teensy 3.0 selected here is code and error.

Code:

unsigned int chrono_id;
#define CHRONO(iterations,function) \
{\
  chrono_id ++;\
  char result[96];\
  sprintf(result, "Chrono %02u - "#function" - "#iterations" calls...\n", chrono_id);\
  Serial.print(result);\
\
  unsigned long time = micros();\
\
  for (unsigned long i = 0; i < iterations; i++)\
    function;\
\
  time = micros() - time;\
\
 Serial.print("Average (microseconds) : ");\
Serial.print((float)time/(float)iterations);\
Serial.print("\n\n");\
}
void setup()
{
  Serial.begin(9600);
  delay(1000);
  
  CHRONO(100000, random(10));
  CHRONO(100000, micros());
  CHRONO(100000,
  {
    sqrt(pow(2, 32));
    sin(cos(tan(123456789)));
  });
}
void loop()
{
}

Error:

c:/users/name/documents/arduino-1.0.1/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.4.1/../../../../arm-none-eabi/bin/ld.exe: section .ARM.exidx loaded at [00007c10,00007c17] overlaps section .data loaded at [00007c10,000081b7]
collect2: ld returned 1 exit status

I see that Teensy 3.0 use an ARM, not an AVR :)

Sorry, can't help with that error :~

guix: I see that Teensy 3.0 use an ARM, not an AVR :)

Sorry, can't help with that error :~

Thank you for the work on this thread, you have helped a bunch!

I e-mailed Paul S...... The Teensy maker. I sent him the code, error, my set-up, and link to this thread. If I made a mistake or if there needs to be a fix, I am sure he will get it.

Edit: see reply #2 for correct code

This code works but, the last result doesn’t look right.

unsigned int chrono_id;
#define CHRONO(iterations,function) \
{\
  chrono_id ++;\
\
  Serial.print("Chrono ");\
  Serial.print(chrono_id);\
  Serial.print(" - "#function" - "#iterations" calls...\n");\
\
  unsigned long time = micros();\
\
  for (unsigned long i = 0; i < iterations; i++)\
  {\
    function;\
  }\
\
  time = micros()-time;\
  Serial.print("Average (microseconds) : ");\
  Serial.print((float)time/(float)iterations);\
  Serial.print("\n\n");\
}

void setup()
{
  Serial.begin(9600);
  delay(5000);
  
  CHRONO(100000, random(10));
  CHRONO(100000, micros());
  CHRONO(100000,
  {
    sqrt(pow(2, 32));
    sin(cos(tan(123456789)));
  });
}
void loop()
{
}

Results Teensy 3.0:

Chrono 1 - random(10) - 100000 calls...
Average (microseconds) : 1.54

Chrono 2 - micros() - 100000 calls...
Average (microseconds) : 1.04

Chrono 3 - { sqrt(pow(2, 32)); sin(cos(tan(123456789))); } - 100000 calls...
Average (microseconds) : 0.00

Results Uno:

Chrono 1 - random(10) - 100000 calls...
Average (microseconds) : 138.69

Chrono 2 - micros() - 100000 calls...
Average (microseconds) : 3.90

Chrono 3 - { sqrt(pow(2, 32)); sin(cos(tan(123456789))); } - 100000 calls...
Average (microseconds) : 0.00

Yes, not sure why, maybe the function isn't actually called if there is nothing to store the result in :~ (I mean, it may be "optimized" when compiling.)

what cpu spedd was the T3.0 running at?

JimEli:
what cpu spedd was the T3.0 running at?

ADAFRUIT has a product description @ http://www.adafruit.com/products/1044

Technical Specifications:
32 bit ARM Cortex-M4 48 MHz CPU (M4 = DSP extensions) Here is Freescale’s reference manual for the chip (warning 1227 pages) as well as the Datasheet and User Guide!
128K Flash Memory, 16K RAM, 2K EEPROM
14* High Resolution Analog Inputs (13 bits usable, 16 bit hardware)
34* Digital I/O Pins (10 shared with analog)
10 PWM outputs
8 Timers for intervals/delays, separate from PWM
USB with dedicated DMA memory transfers
3 UARTs (serial ports)
SPI, I2C, I2S, IR modulator
I2S (for high quality audio interface)
Real Time Clock (with user-added 32.768 crystal and battery)
4 general purpose DMA channels (separate from USB)
Touch Sensor Inputs

cyclegadget: I e-mailed Paul S...... The Teensy maker. I sent him the code, error, my set-up, and link to this thread. If I made a mistake or if there needs to be a fix, I am sure he will get it.

I'm investigating it now.....

Teensy 3.0 has a 48 MHz ARM Cortex-M4 processor. It's the base M4, not a M4F (floating point coprocessor).

For floating point, on ARM "double" is actually a 64 bit number, but on AVR it's a 32 bit float. Many functions default to double. Also, many of the libgcc and newlib functions are not as well optimized as avr-libc. Both of these factors lead to slow-than-expected floating point results.... usually still faster than AVR, but not as much as you might expect from the clock speed alone.

Many years ago I wrote a highly optimized float library for the 8051 processor with the SDCC compiler. Maybe I'll dig into the libraries and do it again? But at the moment, I'm pretty highly focused on just making ARM compatible with huge amounts of code that has only ever been tested on AVR!

Regarding this specific error... linker script issues are always tricky. It might take me a few days. Will post again when I know more.