Arduino Forum

Using Arduino => Programming Questions => Topic started by: Santepeter85 on Oct 16, 2017, 08:32 pm

Title: Speed of an Arduino Mega slower than Arduino Uno?
Post by: Santepeter85 on Oct 16, 2017, 08:32 pm
Hi,
I'm using a 7-segment display to have a 60 minutes countdown clock in a game. If the player makes an error, they get a time penalty (straftijd) where the time will run down a few minutes extra fast (3 minutes in a few seconds).
I've made a code which is exactly the same as the code to run the clock on normal speed (with delay 1000 per second). Now i have reduced and/or even removed the delay during the timepenalty. The original code was on an arduino Uno. Now i am running it on a Mega but the countdown clock isn't going as fast.... It takes about 10 seconds to give a 3 minutes time panelty. On an Uno it takes about 3 seconds)

I've isolated the code for the timepenalty to run in on an Mega and it is going to slow.
Any idea why this is slower than on an Uno?
And any idea how to make this faster (about 3 or 4 times faster atleast)

Code: [Select]

#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"

const byte PIN_CLK = 9;
const byte PIN_DIO = 13;
SevenSegmentExtended display(PIN_CLK, PIN_DIO);

void setup() {
  display.begin();
  display.setBacklight(100);
}

void  loop() {
  byte straftijd = 180;
  byte hours = 60;
  byte minutes = 00;
    for ( ; straftijd > 0 ;) {
      for ( ; hours > 0 ; ) {
        for ( ; minutes > 0; minutes-- && straftijd--) {
          display.printTime(hours, minutes, true);
          if (straftijd == 0) {
            return;
          }
        };
        if (straftijd > 0) {
          minutes = 00;
          display.printTime(hours, minutes, true);  //print seconde 00
          hours--;
          minutes = 59;
      }
    }
  }
}
Title: Re: Speed of an Arduino Mega slower than Arduino Uno?
Post by: lastchancename on Oct 16, 2017, 10:13 pm
At first glance, you are reinitializing straftijd *every* time around loop()

These declarations and initialisers ideally occur either as globals, and/or initialised in setup().  Remember to maintain scope...

Then read/manipulate/reset the value only when needed withinin your loop.
Title: Re: Speed of an Arduino Mega slower than Arduino Uno?
Post by: Santepeter85 on Oct 17, 2017, 04:46 pm
thanks for your help. In this test code the 'straftijd' is reset every loop. In the original code it is set to different values.
The problem is that the counting down is going to slow. Even when there is no delay in the code, it takes more time to count down on a Mega than on an Uno.

Any advise to make this faster?
Title: Re: Speed of an Arduino Mega slower than Arduino Uno?
Post by: econjack on Oct 17, 2017, 05:01 pm
My experience is that most graphics libraries run slower on a Mega than an Uno. Because the same source code runs slower on the Mega, my guess is that it has something to do with difference in data sizes (e.g., Instruction Pointer) the Mega uses to cope with its increased memory resources. I know there are people here who can give you a more definitive answer.
Title: Re: Speed of an Arduino Mega slower than Arduino Uno?
Post by: johnwasser on Oct 17, 2017, 06:08 pm

Code: [Select]
        for ( ; minutes > 0; minutes-- && straftijd--) {

This is a good place to use the comma operator:
Code: [Select]
        for ( ; minutes > 0; minutes-- , straftijd--) {
Title: Re: Speed of an Arduino Mega slower than Arduino Uno?
Post by: westfw on Oct 18, 2017, 09:23 am
The mega will generally be slightly slower than an uno because things like the call and return instructions take extra time and memory to deal with the extra byte in code addresses.  But it shouldn't be 3x slower!