-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d_array_vectors.lua
More file actions
127 lines (107 loc) · 3.36 KB
/
Copy path2d_array_vectors.lua
File metadata and controls
127 lines (107 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- [17:06] <BobbyJones> 2d we just do map[x][y] but for 1d its complicated you would have to find a way to store the coords in there along with the data.
-- [17:12] <BobbyJones> like for example map[x][y] returns {x,y}
-- [17:12] <BobbyJones> so rewriting that would be difficult.
-- [17:10] <+slime> instead of having {{x,y}, {x,y}, {x,y}, {x,y}, ...}, you could have {x,y, x,y, x,y, x,y, ...}
-- [17:12] <+slime> intead you could have it be map[x][y*2+0] returns x, and map[x][y*2+1] returns y
-- [17:18] <+slime> a basic test of a single table-of-1024-vectors saved 50% of the space by doing that
--[[
My results (OS X with 64-bit LuaJIT 2.0.3):
2D array of table vectors: 64.05 MB
2D array of loose vector components: 32.05 MB
linear array of table vectors: 64.00 MB
linear array of loose vector components: 16.00 MB
2D array of struct double vectors: 32.05 MB
2D array of struct float vectors: 24.05 MB
FFI linear array of struct double vectors: 31.69 MB
FFI linear array of struct float vectors: 15.08 MB
]]
local ffi = require("ffi")
ffi.cdef[[
struct DoubleVector { double x, y; };
struct FloatVector { float x, y; };
]]
local newDoubleVector = ffi.typeof("struct DoubleVector")
local newFloatVector = ffi.typeof("struct FloatVector")
local WIDTH = 1024
local HEIGHT = 1024
local function test(name, func)
collectgarbage("collect")
collectgarbage("collect")
local memcount = collectgarbage("count")
func()
local diff = collectgarbage("count") - memcount
print(string.format("%s: %.2f MB", name, diff/1024))
collectgarbage("collect")
end
test("2D array of table vectors", function()
local t = {}
for y=1, HEIGHT do
t[y] = {}
for x=1, WIDTH do
t[y][x] = {love.math.random(), love.math.random()}
end
end
end)
test("2D array of loose vector components", function()
local t = {}
for y=1, HEIGHT do
t[y] = {}
for x=1, WIDTH*2, 2 do
t[y][x*2 + 0] = love.math.random()
t[y][x*2 + 1] = love.math.random()
end
end
end)
test("linear array of table vectors", function()
local t = {}
for i=0, HEIGHT*WIDTH - 1 do
t[i] = {love.math.random(), love.math.random()}
end
-- index using: vec = t[((y - 1) * WIDTH) + (x - 1)]
end)
test("linear array of loose vector components", function()
local t = {}
for i=0, HEIGHT*WIDTH*2 - 1, 2 do
t[i+0] = love.math.random()
t[i+1] = love.math.random()
end
-- index using:
-- x_component = t[(((y - 1) * WIDTH) + (x - 1)) * 2 + 0]
-- y_component = t[(((y - 1) * WIDTH) + (x - 1)) * 2 + 1]
end)
test("2D array of struct double vectors", function()
local t = {}
for y=1, HEIGHT do
t[y] = {}
for x=1, WIDTH do
t[y][x] = newDoubleVector(love.math.random(), love.math.random())
end
end
end)
test("2D array of struct float vectors", function()
local t = {}
for y=1, HEIGHT do
t[y] = {}
for x=1, WIDTH do
t[y][x] = newFloatVector(love.math.random(), love.math.random())
end
end
end)
test("FFI linear array of struct double vectors", function()
local array = ffi.new("struct DoubleVector[?]", HEIGHT*WIDTH)
for y=0, HEIGHT-1 do
for x=0, WIDTH-1 do
array[y*WIDTH + x].x = love.math.random()
array[y*WIDTH + x].y = love.math.random()
end
end
end)
test("FFI linear array of struct float vectors", function()
local array = ffi.new("struct FloatVector[?]", HEIGHT*WIDTH)
for y=0, HEIGHT-1 do
for x=0, WIDTH-1 do
array[y*WIDTH + x].x = love.math.random()
array[y*WIDTH + x].y = love.math.random()
end
end
end)