minetest.get_us_time

From Minetest Developer Wiki
Jump to navigation Jump to search
Mbox warning.png This page contains unofficial, low-quality Lua API documentation and is likely to be outdated or wrong. Do not rely on it!
For the official and up-to-date documentation, see Lua API Documentation.
Mbox warning.png This page has been proposed for deletion for the following reason: "Contains unofficial and potentially outdated, redundant and inconsistent Lua API information"
If you don't think that this page should be deleted, please explain why on the talk page.


Returns system time with microsecond precision. For example, it can be useful for profiling mod performance.
According to the benchmark test (see below) it works about twice as fast as os.clock.


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;


to get time differences, 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