# newbie time problem

hi all, I'm trying to evaluate the time necessary to run a function but seem to have an issue with units. Here's the code :

``````  unsigned long timeNecessary1 = 0;
unsigned long timeNecessary2 = 0;
timeNecessary1 = micros();
IRscan(); // this is my function
timeNecessary2 = micros();
Serial.begin(9600);
Serial.println("");
Serial.println(timeNecessary1,DEC);
Serial.println(timeNecessary2,DEC);
``````

Then i substract one from the other. The problem is the numbers don't make any sense. The results are :

• timeNecessary2 is 2483394992
• timeNecessary1 is 23462246

When i substract one from the other i get 2.12*10^3 seconds wich does not make sense. Do you guys see any issues with the code?

``````micros()
``````

returns the microseconds. 1 micro (u) second = 1/1000000 of a second. So 2.12e3 or 2120 micros actually =s 2120 millionths of a second. This equals 2.12 thousandths of a second, or 2.12 milliseconds, which in turn equals .00212 seconds.

good luck!

Well you need to check your arithmetic: 2483394992 - 23462246 = 2459932746 us = 2.46e3 seconds, not 2.12e3

Also how long does the function actually take? Are you using timer0?

Thanks for quick reply. I do not know how long my function takes, this is why I am doing this test. Is there a better way MarkT?

The code you posted, does it run with interrupts disabled?

i do not know. What does it mean and how can i figure this out? I am not using any delay() if this is what you mean.

Is all the code yours or did you obtain some of it from someone else? If it is all yours then interrupts are not an issue. If you have obtained some of it from another source then search for “sei” and “cli” calls.

Which libraries are you using?

[quote author=Coding Badly link=topic=56098.msg402133#msg402133 date=1300761842] Is all the code yours or did you obtain some of it from someone else? If it is all yours then interrupts are not an issue. If you have obtained some of it from another source then search for "sei" and "cli" calls.

Which libraries are you using?

[/quote]

The code is all mine except for the SoftwareSerial library which I am also using to print the results.

``````Serial.begin(9600);
``````

...is that call inside loop?

[quote author=Coding Badly link=topic=56098.msg402139#msg402139 date=1300763087] ...is that call inside loop?

[/quote] No everything is done only once in Setup. But i did find the problem and my units were wrong. Thanks guys for your support.

matejj: The code is all mine except for the SoftwareSerial library which I am also using to print the results.

SoftwareSerial has been obsolete for a long time. It's time for you to upgrade to NewSoftSerial.