Skip to content

Instantly share code, notes, and snippets.

@ueshita
Created November 6, 2022 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueshita/7d60a47574113c8e5c1b262f36726a9a to your computer and use it in GitHub Desktop.
Save ueshita/7d60a47574113c8e5c1b262f36726a9a to your computer and use it in GitHub Desktop.

Benchmarks of GDScript

Benchmarks Godot3 Godot4 Faster(%)
int_count 52580 30462 72.608
float_count 48105 27786 73.127
array_access 51241 42845 19.596
builtin_function 130234 99035 31.503
type_method 78387 54279 44.415
native_method 169966 25432 568.316
script_function 308276 129272 138.471
script_method 206974 95454 116.831
calc_fib 1323376 633049 109.048
extends SceneTree
func _init():
OS.delay_msec(3000)
bench(funcref(self, "int_count"))
bench(funcref(self, "float_count"))
bench(funcref(self, "array_access"))
bench(funcref(self, "builtin_function"))
bench(funcref(self, "type_method"))
bench(funcref(self, "native_method"))
bench(funcref(self, "script_function"))
bench(funcref(self, "script_method"))
bench(funcref(self, "calc_fib"))
quit()
func bench(target: FuncRef):
var start = OS.get_ticks_usec()
target.call_func()
var end = OS.get_ticks_usec()
var worker_time = (end - start)
print(target.function, ", ", worker_time)
func int_count():
var count: int = 0
for i in range(1000000):
count += 1
func float_count():
var count: float = 0.0
for i in range(1000000):
count += 1.0
func array_access():
var array: Array
array.resize(1000000)
var count: int = 0
for i in range(1000000):
array[i] = i
func builtin_function():
var val: float = 0
for i in range(1000000):
val += sin(float(i))
func type_method():
var string: String = "foobar"
for i in range(1000000):
string.find("foo")
func native_method():
var obj: Reference = Reference.new()
for i in range(1000000/2):
obj.reference()
obj.unreference()
func script_function_target(arg: int) -> int:
return arg + 1
func script_function():
var val: int = 0
for i in range(1000000):
val += script_function_target(i)
class UsersMethod:
func hello():
pass
func script_method():
var ins = UsersMethod.new()
for i in range(1000000):
ins.hello()
func fib(n: int) -> int:
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
func calc_fib():
var list: Array
list.resize(30)
for i in range(30):
list[i] = fib(i)
# print(list)
extends SceneTree
func _init():
OS.delay_msec(3000)
bench(Callable(self, "int_count"))
bench(Callable(self, "float_count"))
bench(Callable(self, "array_access"))
bench(Callable(self, "builtin_function"))
bench(Callable(self, "type_method"))
bench(Callable(self, "native_method"))
bench(Callable(self, "script_function"))
bench(Callable(self, "script_method"))
bench(Callable(self, "calc_fib"))
quit()
func bench(target: Callable):
var start = Time.get_ticks_usec()
target.call()
var end = Time.get_ticks_usec()
var worker_time = (end - start)
print(target.get_method(), ", ", worker_time)
func int_count():
var count: int = 0
for i in range(1000000):
count += 1
func float_count():
var count: float = 0.0
for i in range(1000000):
count += 1.0
func array_access():
var array: Array[int]
array.resize(1000000)
var count: int = 0
for i in range(1000000):
array[i] = i
func builtin_function():
var val: float = 0
for i in range(1000000):
val += sin(float(i))
func type_method():
var string: String = "foobar"
for i in range(1000000):
string.find("foo")
func native_method():
var obj: RefCounted = RefCounted.new()
for i in range(1000000/2):
obj.reference()
obj.unreference()
func script_function_target(arg: int) -> int:
return arg + 1
func script_function():
var val: int = 0
for i in range(1000000):
val += script_function_target(i)
class UsersMethod:
func hello():
pass
func script_method():
var ins = UsersMethod.new()
for i in range(1000000):
ins.hello()
func fib(n: int) -> int:
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
func calc_fib():
var list: Array[int]
list.resize(30)
for i in range(30):
list[i] = fib(i)
# print(list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment