Time Counting

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include  
#include  
int main(void) 
{
int a, b, c;
a=2; b=3;
DDRA=0x00;
i=i&0b00000001
if(i==1)
c=a+b; 
}

How much time will require to execute the program?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably not much time as the optimiser will throw away code that does no useful work.
First up, you havent told us what avr and how fast it is clocked at. Secondly, as mentioned above, your code does nothing useful. In fact, your code won't compile as you havent declared i.

The result of the addition is known at compile time, so that code will reduce to a constant. The result is not used, so that code will be removed. Similarly, if i was declared, its value is knownat compile time so the result of the if is known and would be removed. So about all your code will actually do is the ddra assignment.
If you want to measure execution time, use the Atmel Studio simulator.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The program is not executable, because it will not compile, and is 'illogical' to quote one of my favorite television characters.

If touched up a tad to compile, it will just return zero.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

How much time will require to execute the program?

Even assuming a valid program, given clock speed, AVR model, toolchain options, ... -- what an interesting question!

-- It looks like an AVR program from the chip-include line.
-- When does "program execution" start on an AVR program? At reset, or entering main() or ???
-- When does it end? Never; the catch loop runs forever.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But if you do get it to compile, you can look at the assembler instructions in the lis file. They are 1 or 2 cycles each, so count the instructions and mult by 1.5 to get cycles. Plan B is put that little block in a subroutine and call it in a loop 10,000 times (print out start and stop), time it with a watch, divide result by 10,000.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you edit it to actually 'do something'...

#define F_CPU 16000000
#include 
#include 
int main(void)
{
int a, b, c;
a=2; b=3;
DDRA=0x00;
int i=i&0b00000001;
if(i==1)
c=a+b;

return i;
}

And compile -Os with avr-gcc 4.8.1, you get...

main:
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
	out 0x1,__zero_reg__
	ldi r24,0
	ldi r25,0
	ret

The program then executes in 8 clock cycles.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use the AVR Studio simulator, set a breakpoint to the starting point of your code to be measured, clear the cycle counter, set a 2nd breakpoint at the end of the code-to-be-measured, and let the simulation run. After some seconds it has hit the 2nd breakpoint and you can easily check the number of spent cycles. Converting this number to actual times is a no-brainer once you know your clock frequency.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Plan B is put that little block in a subroutine and call it in a loop 10,000 times

Plan @ (comes before both A and B is the ASCII table): Put the thing into Atmel Studio, run it in the simulator, keep an eye on the Cycle Counter and the Stopwatch.

"The thing" might vary depending on what tool chain you used to build the executable. If you have an ELF file that should be loadable into Atmel Studio. Perhaps a HEX too? I really can't recall..

Anyhow, Studio has the functionality to tell you how many cycles passes during execution. Given the correct info on CPU clock frequency the Stopwatch will also tell you the correct number of micro- or milliseconds.

Quote:
The program then executes in 8 clock cycles.

Not counting startup code...

If we should also count the code executed after main exits, then it is "eternity" or until poweered down.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

And compile -Os with avr-gcc 4.8.1, you get...

Code:
main:
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
out 0x1,__zero_reg__
ldi r24,0
ldi r25,0
ret

The program then executes in 8 clock cycles.


And here I thought that LDI and OUT were one-cycle instructions. Learn something new every day. Perhaps it is an AVR model I am not familiar with.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
and is 'illogical' to quote one of my favorite television characters.

Spock wouldn't have omitted the comma.

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Spock wouldn't have omitted the comma.
Perhaps someone previously in the thread had quoted a television character (who may not have been Spock) ;)

Regards,
Steve A.

The Board helps those that help themselves.