I have an ATmega328P board and i want to measure the number of cycles my code takes. So,
I am using millis() at two points and calculating the time by taking their difference. Is this a correct method?
2.After calculating time, I am multiplying it 16MHz(assuming it is default frequency at which ATmega328P runs) to calculate the number of clockcycles. I am not sure whether I am getting true cycles??
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <avr/pgmspace.h>
#include "cubehash.h"
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int fractional; // variable used to store fractional part of time
void setup(){
Serial.begin(9600);
startTime = millis();
int i,ret_val;
// Serial.flush();
Serial.println("start");
////BASICALLY I WANT TO CALCULATE EXECUTION PERIOD OF
/////FUNCTION gebShortMsg(224);
ret_val=genShortMsg(224);
elapsedTime = millis() - startTime;
// store buttonState in lastButtonState, to compare next time
Serial.print( (long )(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print("."); // print decimal point
// use modulo operator to get fractional part of time
fractional = (long)(elapsedTime % 1000L);
if (fractional == 0)
Serial.print("000"); // add three zero's
else if (fractional < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
Serial.print("00"); // add two zeros
else if (fractional < 100)
Serial.print("0"); // add one zero
Serial.println(fractional); // print fractional part of time
Serial.println("");
}
void loop()
{
}
////////////Then here are the function definitions
i was assuming default speed of atmega328p is 16MHz.
But i looked at the datasheet and it says it is 20MHz.
I am confused because at some places people have taken it as 16MHz.
Can someone please tell me , at what speed my chip is running on( i mean what's the default speed).
The datasheet tells you the maximum speed of the processor, but the actual speed is determined by the speed of the crystal, which is probably a 16MHz one.
Hi all,
I have a doubt , if I am using micros() (or millis()) befor and after the call of the function to measure its execution time , then it also includes time to increment the time counter.
So I am not getting true execution time.
Am I right?
If its so , how can I measure true execution time of the function?
If its so , how can I measure true execution time of the function
Same as always - measure the time it takes to loop 1000 times, measure the time it takes to loop and execute your function 1000 times, subtract one from the other, and divide by 1000.
The time taken to measure the time itself will result in a fairly tiny percentage error.
you mean this: Code:unsigned long time=0;
void setup()
{
Serial.begin(9600);
int i;
time =millis();
for(i=0;i<1000;++i)millis(); Serial.println(millis()-time);
}
void
loop()
{
}
and then divide the o/p by 1000
No.
I mean like:
void setup()
{
Serial.begin(9600);
unsigned long time =millis();
for(int i=0;i<1000;++i); // an empty loop
Serial.println(millis()-time);
time =millis();
for(int i=0;i<1000;++i) {
//your function here
}
Serial.println(millis()-time);
}
void loop(){}
I'm not really interested in how long it takes to call "millis" - I'm only calling it twice.
Oh I think I did not post the question correctly.
By "counter", I didn't mean some loop counter(like "i").
Suppose , I call millis() before and after the function call and get time elapsed as "1234567". It means the chip has incremented the "time counter" 1234567 times and so my question was is the time taken to increment this time counter(i don't know where it is or is it h/w counter or is it s/w counter) included in the 1234567.
It's both h/w and s/w, but done in interrupts, so the relative time taken to increment is largely identical for the empty loop and for the function loop.
If you want, you could run the empty loop for 10000 or 100000 (use a long) times.