- Joined
- Sep 26, 2009
- Messages
- 9,507
[PROVED] Benchmarks are unreliable.
The following benchmark results show something that should be quite revolutionary for current JASS coding:
The following benchmark results show something that should be quite revolutionary for current JASS coding:
JASS:
private function calla2 takes unit u returns nothing
endfunction
private function calla1 takes nothing returns nothing
call calla2(null)
endfunction
// the above function, though it involves a call, is consistently faster than
// the below function. The R2S strings that the benchmark results in
// show, on average 6.5 for the double-function, and 7.5 for the below function.
// that means that passing a single unit through a parameter is about 13%
// faster than declaring that unit as a local and then nulling it. Further tests
// that involve four variables instead of just one, increased the gap so that
// the double-function parameter call was consistently 22-25% faster than the
// declaring/nulling of its counterpart.
private function callb takes nothing returns nothing
local unit u
set u = null
endfunction
JASS:
library Benchmark initializer OnInit
///////////////////////////////////////////////
// Native declarations for stopwatch natives //
// - Requires no modified common.j import //
///////////////////////////////////////////////
native StopWatchCreate takes nothing returns integer
native StopWatchMark takes integer stopwatch returns real
native StopWatchDestroy takes integer stopwatch returns nothing
/////////////////////////
// Benchmarking script //
/////////////////////////
// Initialisation
globals
// ...
endglobals
private function Init takes nothing returns nothing
// things required to be performed once before your test
endfunction
// Tests
globals
private constant string TITLE_A="Test A Name"
//! textmacro Benchmark__TestA
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
call calla1()
//! endtextmacro
private constant string TITLE_B="Test B Name"
//! textmacro Benchmark__TestB
call callb()
call callb()
call callb()
call callb()
call callb()
call callb()
call callb()
call callb()
call callb()
call callb()
//! endtextmacro
endglobals
private function calla2 takes unit u returns nothing
endfunction
private function calla1 takes nothing returns nothing
call calla2(null)
endfunction
private function callb takes nothing returns nothing
local unit u
set u = null
endfunction
// execution
private function TestA1000 takes nothing returns nothing
local integer i=100 // hence 1,000 execs
loop
exitwhen i==0
set i=i-1
// Repeat x10
//! runtextmacro Benchmark__TestA() // 1
//! runtextmacro Benchmark__TestA() // 2
//! runtextmacro Benchmark__TestA() // 3
//! runtextmacro Benchmark__TestA() // 4
//! runtextmacro Benchmark__TestA() // 5
//! runtextmacro Benchmark__TestA() // 6
//! runtextmacro Benchmark__TestA() // 7
//! runtextmacro Benchmark__TestA() // 8
//! runtextmacro Benchmark__TestA() // 9
//! runtextmacro Benchmark__TestA() // 10
endloop
endfunction
private function TestB1000 takes nothing returns nothing
local integer i=100
loop
exitwhen i==0 // hence 1,000 execs
set i=i-1
// Repeat x10
//! runtextmacro Benchmark__TestB() // 1
//! runtextmacro Benchmark__TestB() // 2
//! runtextmacro Benchmark__TestB() // 3
//! runtextmacro Benchmark__TestB() // 4
//! runtextmacro Benchmark__TestB() // 5
//! runtextmacro Benchmark__TestB() // 6
//! runtextmacro Benchmark__TestB() // 7
//! runtextmacro Benchmark__TestB() // 8
//! runtextmacro Benchmark__TestB() // 9
//! runtextmacro Benchmark__TestB() // 10
endloop
endfunction
private function OnEsc takes nothing returns nothing
local integer sw
local integer i
set i=0
set sw=StopWatchCreate()
loop
set i=i+1
call TestA1000.evaluate() // x10 - 10,000 executions altogether.
exitwhen i==10
endloop
call BJDebugMsg(TITLE_A+": "+R2S(StopWatchMark(sw)*100))
call StopWatchDestroy(sw)
set i=0
set sw=StopWatchCreate()
loop
set i=i+1
call TestB1000.evaluate() // x10 - 10,000 executions altogether.
exitwhen i==10
endloop
call BJDebugMsg(TITLE_B+": "+R2S(StopWatchMark(sw)*100))
call StopWatchDestroy(sw)
endfunction
///////////////////////////////
// Registers the OnEsc event //
///////////////////////////////
private function OnInit takes nothing returns nothing
local trigger t=CreateTrigger()
call TriggerRegisterPlayerEvent(t,Player(0),EVENT_PLAYER_END_CINEMATIC)
call TriggerAddAction(t,function OnEsc)
call Init()
endfunction
endlibrary
Last edited: