counting cpu-cycles

By Overflow

Resident (57)

Overflow's picture

16-03-2015, 19:11

Hi again! this is a follow-up for this thread.

For my way of coding (... demo fx), I need(ed) to count cpu-cycles.
I didn't find the feature, I didn't want to ask you guys cos' I should have explained what I was doing.
Now IO is out, I can ask: is there a nicer way to count cpu-cycles that what I've done?

---

Extract from attached thread:
I wrote my own command:
t0 to reset cycles counter,
dt to see how many cycles since the reset.
Prereq:
variable old_time 0
New commands:

proc t0 {} {
	set machineid [machine]
	set err [catch {set mtime [${machineid}::machine_info time]}]
	if {$err} {
		return ""
	}
	set cpu [get_active_cpu]
	set cycle_freq [expr {[machine_info ${cpu}_freq]}]
	set t [expr {$mtime * $cycle_freq}]
	set dt "t="
	append dt [format "%18s" [expr {$t -$::old_time}]]
	append dt "\n                              | "
	set ::old_time $t
	return $dt
}

proc dt {} {
	set machineid [machine]
	set err [catch {set mtime [${machineid}::machine_info time]}]
	if {$err} {
		return ""
	}
	set cpu [get_active_cpu]
	set cycle_period [expr {1.0 / [machine_info ${cpu}_freq]}]
	set t [expr {$mtime / $cycle_period}]
	set dt ""
	append dt "t=     "
	append dt [format "%20s" [expr {$t -$::old_time}]]
	append dt "\n                               | "
	return $dt
}
Login or register to post comments

By Manuel

Ascended (18148)

Manuel's picture

16-03-2015, 20:55

well, most of it is formatting and duplication between the two procs...
If you just care about the current machine and do not care about power off, you can leave out the error checking and the machineid stuff.

It boils down to converting the current time into CPU cycles, and that's this one liner:

proc get_time_in_cycles {} {
  return [expr {[set [get_active_cpu]_freq]} * [machine_info time]]
}

So, if you know time a and time b, just subtract the times and convert.

By Overflow

Resident (57)

Overflow's picture

16-03-2015, 22:26

Wow! thank you. Impressive one-liner. Smile