Go Down

Topic: due serial speed, doing something wrong? (Read 777 times) previous topic - next topic

joeblogs

Hey.
just got a Due.  (org board) used both the org and cc ides.
i threw this sketch onto the board so see what loop times i would end up with.

I have a M0 pro and am getting 897uS from the serial monitor.
The number i am getting back from the Due is 2681uS.

Im sure even the little UNO does it quicker than this.

I have done minimal reading into the due, what is happening?

Code: [Select]
void setup() {
  while (!Serial);
  Serial.begin(115200);

}

void loop() {
 

  static long x;
  static long last;
  x  = ++x;
  while (x == 1000000)
  {
    Serial.println((micros() - last)/1000);
    last = micros();
    x = 0;   
  }

}

HermannSW

#1
Jul 10, 2016, 12:12 pm Last Edit: Jul 10, 2016, 12:24 pm by HermannSW
I can confirm your 2681 on one of my Arduino Due China clones, but it is not micro seconds but milli seconds because you divide by 1000.

You start with unitialized last and x values. More importantly you ask for delay by using loop for time critical measurements, that has a huge overhead. I replaced that with an outer for loop on variable "i" and moved everything into setup():
Code: [Select]
void setup() {
  while (!Serial);
  Serial.begin(115200);

  long i;
  long x=0;
  long last=micros();
  for(i=0; i<10*1000000; ++i)
  {
    x  = ++x;
    if (x ==1000000)
    {
      Serial.println((micros() - last)/1000);
      x = 0;   
      last = micros();
    }
  }
}

void loop() { }


With "static" for last and x I get 345 reported several times.
Removing static for x gives only 238.
Finally removing static from last goes down to 218.
I knew that "volatile" does add delay, seems that "static" does as well.

It is clear that the 1,000,000 loops get executed between measurements, with increments and comparisons for both "x" and "i". 218ms means 218*84000/1000000=18.3 clock cycles per 1 of the million loops.


Ooops:
Did above measurements on a freshly installed laptop, and hat had "optimize for speed" (-Os) set in platform.txt. I changed the 3 occurences of "-Os" by "-O3" to optimize for speed, restarted Arduino IDE and compiled and flushed and run above code again. This time 119ms gets reported, with 119*84000/1000000=9.996 clock cycles per loop. This really is 10 clock cycles and the smaller value gets explained by the integer division by 1000 in code.

I do use -O3 always in platform.txt for my DUEs, no need for optimization by size with Arduino Due.

 
Hermann.
https://forum.arduino.cc/index.php?topic=462107.msg3236016#msg3236016
http://stamm-wilbrandt.de/en/Raspberry_camera.html

joeblogs

Hey Hermann,
cheers for the reply.
the whole idea was to measure the loop time, blank or full of code.
Sorry my mistake ms not uS.

im new to this, but even though last and x were floating, once through the counter wouldnt they all be set to where the should be. so the first loop might be wrong but every one after sould be correct?

what is this magical stuff you speak of?
Quote
Did above measurements on a freshly installed laptop, and hat had "optimize for speed" (-Os) set in platform.txt. I changed the 3 occurences of "-Os" by "-O3" to optimize for speed, restarted Arduino IDE and compiled and flushed and run above code again
can you point me to some pages so i can do some reading.

im still a bit confused why the due would be 4 x slower than a zero, running the exact same code. Slower than the UNO even.

On a side note, i have used some cheap chinese plcs to do some basic stuff, easy plcThey use function blocks).  similar specs to an uno, and one program i wrote pretty much filled up the chip and i waas getting 7.3ms loop times.  Why is arduino slower is it because of the programming language?



joeblogs

just out of curiosity i ran your code with a few variations, and uploaded using Atmel Studio 7.

programmed in Atmel Studio 7 (DUE)

Code placed in the loop
static variables, result = 548
no static variables, result = 225

Code placed in the setup
static variables, result = 536
no static variables, result = 218

seems static variables slow the times a fair bit.

the no static results in the loop and setup seem correct to me as im sure ive read that the loop code takes about what im seeing as the difference between the 2.

is it just me, or do the times differ a fair bit depending on which IDE you use.

HermannSW

Hi,

under Linux with Arduino IDE you find platform.txt here:
Code: [Select]
~/.arduino15/packages/arduino/hardware/sam/1.6.8/platform.txt

As said I replaced the three occurences of -Os by -O3.

> is it just me, or do the times differ a fair bit depending on which IDE you use.
>
I don't think it is the IDE itself, but the parameters and settings passed to the compiler that produces the machine code. I would bet that if you configure Arduino IDE and Atmel Studio 7 so that both times the same parameter set gets passed to the compiler (you can see with "Verbose output" in Arduino IDE), then you will get identical machine code.

Hermann.
https://forum.arduino.cc/index.php?topic=462107.msg3236016#msg3236016
http://stamm-wilbrandt.de/en/Raspberry_camera.html

pjrc

im still a bit confused why the due would be 4 x slower than a zero, running the exact same code.
Maybe it's related to the larger number of serialEvents which need to be checked between each time loop() runs?

agehall

You are using the programming port which seems to be totally unusable for anything but programming. Just use the other port and all should be fine.

Okio

im new to this, but even though last and x were floating, once through the counter wouldnt they all be set to where the should be. so the first loop might be wrong but every one after sould be correct?

In this respect your code was not incorrect. This is because the C spec states that static variabless are automatically initialised to 0 (or memset(addr, 0, sizeof(var)). Therefore the initial value of both x and last is 0.

Also, for a single file, there is no need to modify platform.txt to adjust compiler flags. In this case adding '#pragma GCC optimize ("-O3")' to the begging of any file will prompt GCC to apply said flag(s) to and for this file only.
Also note that -O3 are considered to be "unsafe" (not for production use) whereas "-O2" is considered safe to use.


I'm curious. What is your use-case where loop processing time is your bottleneck?

joeblogs

Quote
using the programming port which seems to be totally unusable for anything but programming
Agehall you are on the money, seems the arduino IDE doesnt like the programming port too much, AS7 seemed to work better.  using the native port gets results that you would expect.

hey Okio
no bottleneck, just trying to figure what works and what doesnt.
I think ill leave all the compiler stuff alone for now till im a bit better at C++

thanks for the replys people.


Go Up