Pages: [1]   Go Down
Author Topic: Same line of code takes drastically different times to execute (Due)  (Read 91 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
double answer = int2sinescale(input, minADC, maxADC) * int2sinescale(scaledsine[i], 0, 4095);
OK so when I wrap some timing code around it in the actual loop where it's being used, I get
Quote
28 microsecs (lockamp)
28 microsecs (lockamp)
27 microsecs (lockamp)
28 microsecs (lockamp)
28 microsecs (lockamp)
19 microsecs (lockamp)
28 microsecs (lockamp)

But when I just copy that line, put it into a shell command called "benchmark" (it looks like this):
Code:
    starttime = micros();
    double voltage = int2voltage(800, 0.55, 2.75);
    elapsedtime = micros() - starttime;
    Serial.print(elapsedtime); Serial.println(" microsecs (int2voltage)");
    starttime = micros();
    int sinescale = int2sinescale(2200, 688, 3424);
    elapsedtime = micros() - starttime;
    Serial.print(elapsedtime); Serial.println(" microsecs (int2sinescale)");
    starttime = micros();
    double answer = 0.55 * 2.7555324;
    elapsedtime = micros() - starttime;
    Serial.print(elapsedtime); Serial.println(" microsecs (multiply two doubles)");
    starttime = micros();
    voltage = int2sinescale(700, minADC, maxADC) * int2sinescale(scaledsine[3], 0, 4095);
    elapsedtime = micros() - starttime;
    Serial.print(elapsedtime); Serial.println(" microsecs (lockamp int2sinescale line)");
Quote
auteur > benchmark
1 microsecs (int2voltage)
1 microsecs (int2sinescale)
1 microsecs (multiply two doubles)
2 microsecs (lockamp int2sinescale line)

What the hell? None of these report such low execution times when I put timing code around them where they're actually being used in the program. What's actually going on?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26472
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're timing the serial output?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7612
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


OK so when I wrap some timing code around it in the actual loop where it's being used, I get

But when I just copy that line, put it into a shell command called "benchmark" (it looks like this):

What's actually going on?

How would anyone know?   You haven't posted the code that gives the long times.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As I said, it's just that same line of code.

It turned out that since I wasn't using the value, the compiler optimized it away. The asm("") trick that works on AVR Arduinos doesn't work, so I had to Serial.print the answers out. Thanks anyway!
Logged

Pages: [1]   Go Up
Jump to: