Posts: 1
Joined: Tue Mar 25, 2014 2:38 pm

timing using CPU cycles at runtime

Mon Mar 31, 2014 4:54 pm


I followed instructions at http://blog.regehr.org/archives/794 and am able to count CPU cycles. My question is how can I convert these numbers into a "time" value? I naively assumed that if I knew the CPU clock speed, then dividing the number of (counted) CPU cycles by the clock speed would give me time in seconds. I am now not sure this is true. Any pointers?

Also, while we are here - is there a way to get the clock speed at runtime (via a library call?) or must I read from /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq?



Posts: 1337
Joined: Sat Dec 17, 2011 5:05 pm

Re: timing using CPU cycles at runtime

Tue Apr 01, 2014 2:40 am

You could just use 'man time' but if you turn turbo and used a real time os then your cycles might relate to time.
SBC with 32GB RAM: https://hardkernel.com

FAQ : https://raspberrypi.stackexchange.com

Unanswered: https://www.raspberrypi.org/forums/search.php?search_id=unanswered

User avatar
Posts: 80
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
Contact: Website

Re: timing using CPU cycles at runtime

Tue Apr 01, 2014 2:13 pm

I wasn't aware that there was a CPU cycle counter on the Raspi but I did know of the 1us hardware timer/counter on the PI which is accessible through a driver (which I have used and works well). I am not sure if were talking about the same thing or I am lacking some knowledge about a different one.

Usually people use the hardware real time clock to initially calibrate their time counts so that they can then proceed to use it. Since the Pi doesnt have one you could use an internet based clock service like the US tycho (http://tycho.usno.navy.mil/cgi-bin/timer.pl) and run it for a long time - enough to compensate for the web page latency - and see how many counts it generates and divide that. I would think all of the system calls for collecting the time would be based on the CPU counter time which is a bit incestuous.

It depends on how accurate you need to know it. If your timing short function calls like in a debug tracer then its not so much of an issue.

Return to “C/C++”