Difference between revisions of "minetest.get us time"
Jump to navigation
Jump to search
(→Description: (last edit and this:) Information retrieved from lua_api.txt and from in-game testing.) |
Hybrid Dog (talk | contribs) |
||
Line 6: | Line 6: | ||
Returns time with microsecond precision. This does not seem to be strictly related to [[minetest.get_gametime|game time]] or [[timeofday|time of day]] in a meaningful way, but should be useful for taking differences in time measured during a single execution of the server. For example, it can be useful for profiling mod performance. | Returns time with microsecond precision. This does not seem to be strictly related to [[minetest.get_gametime|game time]] or [[timeofday|time of day]] in a meaningful way, but should be useful for taking differences in time measured during a single execution of the server. For example, it can be useful for profiling mod performance. | ||
− | == | + | == Examples == |
<source> | <source> | ||
local t0_us = minetest.get_us_time(); | local t0_us = minetest.get_us_time(); | ||
Line 13: | Line 13: | ||
local elapsed_time_in_seconds = (t1_us - t0_us)/1000000.0; | local elapsed_time_in_seconds = (t1_us - t0_us)/1000000.0; | ||
</source> | </source> | ||
+ | |||
+ | minetest.get_us_time seems to works faster than os.clock: | ||
+ | <source>[…] | ||
+ | |||
+ | local oclock = os.clock | ||
+ | local ustime = minetest.get_us_time | ||
+ | |||
+ | local function test_os_clock() | ||
+ | local t1 = oclock() | ||
+ | local delay = tonumber(oclock() - t1) | ||
+ | end | ||
+ | |||
+ | local function test_get_us_time() | ||
+ | local t1 = ustime() | ||
+ | local delay = (ustime() - t1) / 1000000 | ||
+ | end | ||
+ | |||
+ | local function apply_timer_tests() | ||
+ | local oclock_count = benchmark_function(test_os_clock) | ||
+ | local ustime_count = benchmark_function(test_get_us_time) | ||
+ | print("test_os_clock: " .. oclock_count .. " s⁻¹") | ||
+ | print("test_get_us_time: " .. ustime_count .. " s⁻¹") | ||
+ | print("minetest.get_us_time is " .. ustime_count / oclock_count .. " times as fast as os.clock") | ||
+ | end | ||
+ | |||
+ | apply_timer_tests() | ||
+ | |||
+ | minetest.register_node(":mo:timtest", { | ||
+ | on_place = apply_timer_tests | ||
+ | }) | ||
+ | |||
+ | --[[ results: | ||
+ | |||
+ | Using minetest.get_us_time for measuring (see the benchmark function): | ||
+ | |||
+ | executing when loading: | ||
+ | |||
+ | test_os_clock: 1019720 s⁻¹ | ||
+ | test_get_us_time: 1843077.3333333 s⁻¹ | ||
+ | minetest.get_us_time is 1.8074347206423 times as fast as os.clock | ||
+ | |||
+ | |||
+ | placing the node: | ||
+ | |||
+ | test_os_clock: 742641.66666667 s⁻¹ | ||
+ | test_get_us_time: 1772569.6666667 s⁻¹ | ||
+ | minetest.get_us_time is 2.3868438120673 times as fast as os.clock | ||
+ | |||
+ | test_os_clock: 824095 s⁻¹ | ||
+ | test_get_us_time: 1790176.6666667 s⁻¹ | ||
+ | minetest.get_us_time is 2.1722940518589 times as fast as os.clock | ||
+ | |||
+ | test_os_clock: 736386.33333333 s⁻¹ | ||
+ | test_get_us_time: 1769226.6666667 s⁻¹ | ||
+ | minetest.get_us_time is 2.4025794431275 times as fast as os.clock | ||
+ | |||
+ | |||
+ | Using os.clock for measuring: | ||
+ | |||
+ | executing when loading: | ||
+ | |||
+ | test_os_clock: 801913.66666667 s⁻¹ | ||
+ | test_get_us_time: 1229572.3333333 s⁻¹ | ||
+ | minetest.get_us_time is 1.5332976409347 times as fast as os.clock | ||
+ | |||
+ | |||
+ | placing the node: | ||
+ | |||
+ | test_os_clock: 326955 s⁻¹ | ||
+ | test_get_us_time: 642647.66666667 s⁻¹ | ||
+ | minetest.get_us_time is 1.9655538733669 times as fast as os.clock | ||
+ | |||
+ | test_os_clock: 475252 s⁻¹ | ||
+ | test_get_us_time: 844550.33333333 s⁻¹ | ||
+ | minetest.get_us_time is 1.7770579257601 times as fast as os.clock | ||
+ | |||
+ | test_os_clock: 296636 s⁻¹ | ||
+ | test_get_us_time: 574708.33333333 s⁻¹ | ||
+ | minetest.get_us_time is 1.9374193736881 times as fast as os.clock | ||
+ | ]] | ||
+ | </source> | ||
+ | benchmark_function from [[Lua_Optimization_Tips#Benchmarking]] | ||
+ | |||
+ | <br/> | ||
[[Category:Methods]] | [[Category:Methods]] |
Revision as of 16:45, 21 April 2016
Syntax
minetest.get_us_time()
Description
Returns time with microsecond precision. This does not seem to be strictly related to game time or time of day in a meaningful way, but should be useful for taking differences in time measured during a single execution of the server. For example, it can be useful for profiling mod performance.
Examples
local t0_us = minetest.get_us_time();
-- ...
local t1_us = minetest.get_us_time();
local elapsed_time_in_seconds = (t1_us - t0_us)/1000000.0;
minetest.get_us_time seems to works faster than os.clock:
[…]
local oclock = os.clock
local ustime = minetest.get_us_time
local function test_os_clock()
local t1 = oclock()
local delay = tonumber(oclock() - t1)
end
local function test_get_us_time()
local t1 = ustime()
local delay = (ustime() - t1) / 1000000
end
local function apply_timer_tests()
local oclock_count = benchmark_function(test_os_clock)
local ustime_count = benchmark_function(test_get_us_time)
print("test_os_clock: " .. oclock_count .. " s⁻¹")
print("test_get_us_time: " .. ustime_count .. " s⁻¹")
print("minetest.get_us_time is " .. ustime_count / oclock_count .. " times as fast as os.clock")
end
apply_timer_tests()
minetest.register_node(":mo:timtest", {
on_place = apply_timer_tests
})
--[[ results:
Using minetest.get_us_time for measuring (see the benchmark function):
executing when loading:
test_os_clock: 1019720 s⁻¹
test_get_us_time: 1843077.3333333 s⁻¹
minetest.get_us_time is 1.8074347206423 times as fast as os.clock
placing the node:
test_os_clock: 742641.66666667 s⁻¹
test_get_us_time: 1772569.6666667 s⁻¹
minetest.get_us_time is 2.3868438120673 times as fast as os.clock
test_os_clock: 824095 s⁻¹
test_get_us_time: 1790176.6666667 s⁻¹
minetest.get_us_time is 2.1722940518589 times as fast as os.clock
test_os_clock: 736386.33333333 s⁻¹
test_get_us_time: 1769226.6666667 s⁻¹
minetest.get_us_time is 2.4025794431275 times as fast as os.clock
Using os.clock for measuring:
executing when loading:
test_os_clock: 801913.66666667 s⁻¹
test_get_us_time: 1229572.3333333 s⁻¹
minetest.get_us_time is 1.5332976409347 times as fast as os.clock
placing the node:
test_os_clock: 326955 s⁻¹
test_get_us_time: 642647.66666667 s⁻¹
minetest.get_us_time is 1.9655538733669 times as fast as os.clock
test_os_clock: 475252 s⁻¹
test_get_us_time: 844550.33333333 s⁻¹
minetest.get_us_time is 1.7770579257601 times as fast as os.clock
test_os_clock: 296636 s⁻¹
test_get_us_time: 574708.33333333 s⁻¹
minetest.get_us_time is 1.9374193736881 times as fast as os.clock
]]
benchmark_function from Lua_Optimization_Tips#Benchmarking