5

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.

dda
  • 1,595
  • 1
  • 12
  • 17
EEd
  • 904
  • 1
  • 14
  • 21

4 Answers4

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.

Majenko
  • 105,851
  • 5
  • 82
  • 139
5

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.

Jot
  • 3,276
  • 1
  • 14
  • 21
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.

dda
  • 1,595
  • 1
  • 12
  • 17
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.

JRobert
  • 15,407
  • 3
  • 24
  • 51