3

I am designing a hard real-time data logger. The Uno is a bit marginal in processing speed.

a) Would it be faster if I changed to an ATmega2560?

b) On the datasheet, ATMEGA2560 has 135 instructions; it is 4 instructions more than the ATmega328p.

What are the extra 4 instructions for?

Are they involved in floating point maths?

c) How to select compiler option for fastest code at the expense of bigger code size, longer compilation time, etc.?

The system is:

  1. Receive data from wireless transceiver chip via hardware SPI port.

  2. A good portion of the time is spent on data calculation using floating point maths.

  3. Output processed data on serial port (then to PC via the Arduino board USB to Serial chip).

  4. Code and RAM size is not an issue, Uno has plenty space left.

  • 4
    The Uno is actually faster than the Mega, especially if there is a high frequency of function calls and return. Easiest way to check this is to simply read the number of clock cycles for typical instruction sequences. – Mikael Patel Mar 31 '17 at 22:52
  • 2
    If you need speed replace the floating point math with fixed point. And to reduce latency pipeline input/output with the processing. – Mikael Patel Mar 31 '17 at 22:52
  • 2
    If you are concerned about performance, don't use an ATmega-based Arduino at all. Today's ARM parts, from many vendors (even Atmel themselves) are many times faster, no more expensive, work better on low battery voltages, and several have Arduino ports. – Chris Stratton Apr 1 '17 at 7:01
  • If it's connected to a PC, then you could just offload the calculation to the PC. But I'd first do some measurements and see if there is even a problem, and where this problem lays. Could you for example speed up the SPI clock. Doing optimizations without measurements is just silly, as you don't know if they worked, or if they made things even worst. – Gerben Apr 1 '17 at 10:13
  • @Chris Stratton, could you please list some example parts, if it is not against the 'rules' for this forum. (Can I ask this under forum rules) Is STM323F1xx a good choice? assume able to use its (non Arduino) development package and IDE STM32CubeMX. – EEd Apr 3 '17 at 21:16
8

No, it will not be any faster. The extra instructions are to access the extra flash memory of the 2560. Instructions like EIJMP - Extended indirect jump, EICALL - Extended indirect call, etc.

The compiler option -O3 optimises the most, but there are other options you can enable, such as -funroll-loops which will cause major code bloat but speed things up. Read the GCC manual pages to boggle at all the millions of optimisation options.

Or... use a faster chip, like an ARM or PIC32.

6

Use the newest Arduino IDE, because some time ago the 'lto' option was added which is a major optimization for both speed and size.
It is possible to test a few compiler options with the #pragma. Try for example these at the top of the sketch:

#pragma GCC optimize("-O3")
#pragma GCC optimize("-ffast-math")

The 'fast-math' is dangerous, be careful with it.

The Arduino Zero and Arduino Due are faster but not a lot faster, because they are not fast with analogRead() and with floating point calculations.

A common mistake is that someone asks for a faster Arduino board, but uses a sketch and libraries with a lot of delay() in it. That won't be any faster with an other Arduino board. Have a good look at your code and at the source code of the used libraries.

Have you tried increasing the baudrate for outgoing data ? If that baudrate is slow, the whole sketch is slowed down because the Serial functions will wait until they can write the data into the Serial buffer. The fastest "baudrate" can be achieved with the Leonardo, Micro and Pro Micro boards. They transmit the data directly onto the USB bus without using the baudrate.

3

In the platform.txt file found in ~/Arduino15/packages/arduino/hardware/avr/1.6.17 or similar, you can change the three instances of -Os with -Ofast. The size of the code can increase dramatically though.

1

They have very nearly the same instruction set and the same execution speed, so there will be no increase there. If your project spends a significant amount of time in floating-point arithmetic, replacing it with fixed point arithmetic will give you a noticeable speed up. If a few calculations are executed repeatedly (which is typical), you can get most of the advantage by rewriting only that part.

In a nutshell, fixed point arithmetic is what the computer does already, but with an assumed binary point (just like a decimal point) always to the right of the least significant bit. Fixed point arithmetic uses the built-in arithmetic, but you assume the binary point to be wherever you need it, keep track of it during multiplies and divides, and make sure you keep the intermediate results from overflowing.

Search for Fixed Point Arithmetic and you'll find out a whole lot more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.