User:Ackis/LUACode
From WoWWiki
| | This article or section is a player stub. You can help expand it by editing it.
|
Contents |
LUA Code Snippets
Passing Tables
LUA code
globaldb = {}
for i=1,10000 do
globaldb[i] = "Foo"
end
do
local scopedb = {}
for i=1,10000 do
scopedb[i] = "Foo"
end
function dostuff(DB)
for i=1,1000,1 do
DB[i] = "foo"
end
end
function testpassing()
local starttime,endtime,localpass,scopepass,globalpass
local localdb = {}
for i=1,10000,1 do
localdb[i] = "Foo"
end
starttime = os.clock()
for i=1,10000,1 do
dostuff(localdb)
end
endtime = os.clock()
localpass = endtime - starttime
starttime = os.clock()
for i=1,10000,1 do
dostuff(scopedb)
end
endtime = os.clock()
scopepass = endtime - starttime
starttime = os.clock()
for i=1,10000,1 do
dostuff(globaldb)
end
endtime = os.clock()
globalpass = endtime - starttime
print("Local: " .. localpass)
print("Scope: " .. scopepass)
print("Global: " .. globalpass)
end
end
globalref = {}
for i=1,1000 do
globalref[i] = "Foo"
end
do
local scopedb = {}
for i=1,1000 do
scopedb[i] = "Foo"
end
function dostuffglobal()
for i=1,1000,1 do
globalref[i] = "Foo"
end
end
function dostuffscope()
for i=1,1000,1 do
scopedb[i] = "Foo"
end
end
function testref()
local starttime,endtime,scopepass,globalpass
starttime = os.clock()
for i=1,10000,1 do
dostuffscope()
end
endtime = os.clock()
scopepass = endtime - starttime
starttime = os.clock()
for i=1,10000,1 do
dostuffglobal()
end
endtime = os.clock()
globalpass = endtime - starttime
print("Scope: " .. scopepass)
print("Global: " .. globalpass)
end
end
print("Testing by passing arguments.")
testpassing()
print("Testing by reference.")
testref()
Results
Testing by passing arguments. Local: 0.453 Scope: 0.469 Global: 0.438 Testing by reference. Scope: 0.594 Global: 0.718
Testing by passing arguments. Local: 0.438 Scope: 0.453 Global: 0.453 Testing by reference. Scope: 0.563 Global: 0.734
Testing by passing arguments. Local: 0.453 Scope: 0.453 Global: 0.453 Testing by reference. Scope: 0.563 Global: 0.734
Testing by passing arguments. Local: 0.453 Scope: 0.453 Global: 0.453 Testing by reference. Scope: 0.563 Global: 0.734
Testing by passing arguments. Local: 0.453 Scope: 0.453 Global: 0.469 Testing by reference. Scope: 0.562 Global: 0.735
Testing by passing arguments. Local: 0.453 Scope: 0.453 Global: 0.469 Testing by reference. Scope: 0.562 Global: 0.735
Summary
Passing via argument doesn't lead any performance benefit and is faster than passing via scope or global
Nested If Statements
LUA code
function testifs(a,b,c,d)
local starttime,endtime,nested,notnested
local dumpvar
starttime = os.clock()
for i=1,1000000,1 do
if (a) then
if (b) then
if (c) then
if (d) then
dumpvar = i
end
end
end
end
end
endtime = os.clock()
nested = endtime - starttime
starttime = os.clock()
for i=1,1000000,1 do
if (a and b and c and d) then
dumpvar = i
end
end
endtime = os.clock()
notnested = endtime - starttime
print("Nested if: " .. nested)
print("Non-nested if: " .. notnested)
print("Difference: " .. (nested - notnested))
end
LUA Code List
function testifs(a,b,c,d)
(62 instructions, 248 bytes at 00191630, 4 params, 13 slots, 0 upvalues, 17 locals, 8 constants, 0 functions)
2 [1] SETGLOBAL 0 -1 ; testifs
local starttime,endtime,nested,notnested
local dumpvar
starttime = os.clock()
1 [5] GETGLOBAL 9 -1 ; os
2 [5] GETTABLE 9 9 -2 ; "clock"
3 [5] CALL 9 1 2
4 [5] MOVE 4 9
for i=1,1000000,1 do
5 [6] LOADK 9 -3 ; 1
6 [6] LOADK 10 -4 ; 1000000
7 [6] LOADK 11 -3 ; 1
8 [6] FORPREP 9 9 ; to 18
18 [6] FORLOOP 9 -10 ; to 9
if (a) then
9 [7] TEST 0 0 0
10 [7] JMP 7 ; to 18
if (b) then
11 [8] TEST 1 0 0
12 [8] JMP 5 ; to 18
if (c) then
13 [9] TEST 2 0 0
14 [9] JMP 3 ; to 18
if (d) then
15 [10] TEST 3 0 0
16 [10] JMP 1 ; to 18
dumpvar = i
17 [11] MOVE 8 12
end
end
end
end
end
endtime = os.clock()
19 [18] GETGLOBAL 9 -1 ; os
20 [18] GETTABLE 9 9 -2 ; "clock"
21 [18] CALL 9 1 2
22 [18] MOVE 5 9
nested = endtime - starttime
23 [19] SUB 6 5 4
starttime = os.clock()
24 [21] GETGLOBAL 9 -1 ; os
25 [21] GETTABLE 9 9 -2 ; "clock"
26 [21] CALL 9 1 2
27 [21] MOVE 4 9
for i=1,1000000,1 do
28 [22] LOADK 9 -3 ; 1
29 [22] LOADK 10 -4 ; 1000000
30 [22] LOADK 11 -3 ; 1
31 [22] FORPREP 9 9 ; to 41
41 [22] FORLOOP 9 -10 ; to 32
if (a and b and c and d) then
32 [23] TEST 0 0 0
33 [23] JMP 7 ; to 41
34 [23] TEST 1 0 0
35 [23] JMP 5 ; to 41
36 [23] TEST 2 0 0
37 [23] JMP 3 ; to 41
38 [23] TEST 3 0 0
39 [23] JMP 1 ; to 41
dumpvar = i
40 [24] MOVE 8 12
end
end
endtime = os.clock()
42 [28] GETGLOBAL 9 -1 ; os
43 [28] GETTABLE 9 9 -2 ; "clock"
44 [28] CALL 9 1 2
45 [28] MOVE 5 9
notnested = endtime - starttime
46 [29] SUB 7 5 4
print("Nested if: " .. nested)
47 [30] GETGLOBAL 9 -5 ; print
48 [30] LOADK 10 -6 ; "Nested if: "
49 [30] MOVE 11 6
50 [30] CONCAT 10 10 11
51 [30] CALL 9 2 1
print("Non-nested if: " .. notnested)
52 [31] GETGLOBAL 9 -5 ; print
53 [31] LOADK 10 -7 ; "Non-nested if: "
54 [31] MOVE 11 7
55 [31] CONCAT 10 10 11
56 [31] CALL 9 2 1
print("Difference: " .. (nested - notnested))
57 [32] GETGLOBAL 9 -5 ; print
58 [32] LOADK 10 -8 ; "Difference: "
59 [32] SUB 11 6 7
60 [32] CONCAT 10 10 11
61 [32] CALL 9 2 1
Summary
LUA is smart enough to code both nested if's and multiple boolean parameters the same. Code whichever is better for readability.
