Go Down

Topic: Same line of code takes drastically different times to execute (Due) (Read 228 times) previous topic - next topic

randomshinichi

Code: [Select]
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: [Select]
    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?

AWOL

"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.

Robin2



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

randomshinichi

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!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy