-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose cpu ccount registrer as platform and lua_node functions #2906
Conversation
Nikolay, IMO the ccount register is useful if you are developing exceptionally time critical C code libraries, and such developers should be aware of how to use it. But for the existing Lua applications developers, I feel that the existing For example: a={}
for j=1,5 do
local t0=tmr.now()
for i=1,10 do
local t1=tmr.now()
a[i]=t1-t0
t0=t1
end
print(table.concat(a,' '))
end
160 216 37 122 28 121 32 28 27 121
41 28 27 27 27 27 28 27 27 27
41 27 27 28 27 27 27 27 28 27
41 27 27 28 27 27 27 27 28 27
41 27 27 28 27 27 27 27 28 27 OK, this is running under Lua 5.3 (which is what is on the Wemos chip plugged into my laptop at the moment). But the core 8 Lua VM opcode for loop (lines 4-7) is clocking in at around 27uSec or just over 3uSec per opcode.
(The hickups on the first iteration are as the VM grows the table). |
And here are the results with the int32 / float (32-bit) build: the size and runtime advantages of the Int build but with the flexibility to do single precision FP calcs if you need to.
😄 |
@TerryE should your valuable feedback be seen as a nay wrt to merging this? I don't have any objections to adding this new function. |
Marcel, the |
You're definitely right about the Lua overhead distorting the results. Feel free to close the PR. Would be interesting to see a side-by-side comparison of |
side by side both ways using wemos, latest dev-branch 32-float build: a={}
for j=1,5 do
local t0=tmr.now()
for i=1,10 do
local t1=tmr.now()
a[i]=t1-t0
t0=t1
end
print(table.concat(a,' '))
end
119 1258 712 710 93 715 93 98 93 717
111 129 89 90 89 90 89 89 90 89
112 121 89 89 90 89 89 90 89 90
108 124 90 89 90 89 89 90 89 89
112 121 89 89 90 89 89 90 89 90
'
a={}
for j=1,5 do
local t0=node.ccount()
for i=1,10 do
local t1=node.ccount()
a[i]=t1-t0
t0=t1
end
print(table.concat(a,' '))
end
9246 99727 56637 56755 7467 57165 7468 7792 7467 57813
9573 9672 7146 7155 7146 7146 7146 7155 7146 7146
8939 9986 7147 7155 7146 7146 7146 7156 7146 7146
9254 9672 7146 7155 7146 7146 7146 7155 7146 7146
8939 9986 7147 7155 7146 7146 7147 7155 7146 7146 ccount values are 80MHZ counters. they have to be multiplied with 0.0125 to get them in us.
|
@TerryE : i think your view on this is a bit too narrow seeing it as shootout between ccount and tmr.now only. if i look from that angle alone, i'd add that ccount is not lua-portable function and being there only would extend the tech debt without clear benefit. these are all fair and square but i see some other angles as well. ccount function allows to compare c and lua duration results directly, no need of metrics conversion. for c-module development i think this is great. and not to forget, ccount arithmetic doesn't suffer from 31-bit rollover of tmr.now.
;) |
Nikolay, my vote is a weak -1, so I am happy to be outvoted on this. That being said, if we do add a |
i've moved the method to tmr package as suggested by terry. benchmarking lua functions with it: function timeIt(fnc, cnt)
local function loopIt(f2)
local t0 = tmr.ccount()
for i=1,cnt do
f2()
end
local t1 = tmr.ccount()
return math.ceil((t1-t0)/cnt)
end
assert(type(fnc) == "function", "function to test missing")
cnt = cnt or 1000
local emptyTime = loopIt(function()end)
local deltaCPUTicks = math.abs(loopIt(fnc) - emptyTime)
local deltaUS = math.ceil(deltaCPUTicks/node.getcpufreq())
return deltaCPUTicks, deltaUS
end
print( timeIt(function() end) )
6 1
print( timeIt(function() tmr.ccount() end) )
6274 79
print( timeIt(function() tmr.now() end) )
6325 80 same logic works just fine with tmr.now() instead, without the extra precision of ccount of course. |
@marcelstoer if there is no other person sharing my taste in cpu timing ;), i think we can close this request and forget about it. i'll keep it in a branch of my own rather than push it to master... |
I'm actually fine to expose this. It's up to the user to use or not use it. As Terry only gave a weak -1 I'll merge it. Thanks for your efforts. |
dev
branch rather than formaster
.docs/*
.Since I've used time duration with microsecond precision in c code, I kind of missed it in lua.
This request is exposing CCOUNT register value as new lua method to "node" module.
And also declares it as c function in "plarform.h" to allow for future removal of duplicated declarations.
I'll appreciate some feedback, especially if these are not appropriate places for such method.