-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbench.lua
76 lines (66 loc) · 2.03 KB
/
bench.lua
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
local base64 = require'base64'
local N = 10000000
local st = {}
local letters = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
.. 'абвгдеёжзийклмнопрстуфхцшщчъыьэюя'
.. 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦШЩЧЪЫЬЭЮЯ'
local nletters = #letters
for i = 1, N do
local j = math.random( nletters )
st[i] = letters:sub( j, j )
end
local s = table.concat( st )
local t = os.clock()
local encoded = base64.encode( s )
local encodetime = os.clock() - t
t = os.clock()
local decoded = base64.decode( encoded )
local decodetime = os.clock() - t
assert( s == decoded )
print('Common text')
print(('Encoding: %d bytes/sec'):format( math.floor(N/encodetime)))
print(('Decoding: %d bytes/sec'):format( math.floor(N/decodetime)))
collectgarbage()
t = os.clock()
encoded = base64.encode( s, nil, true )
encodetime = os.clock() - t
t = os.clock()
decoded = base64.decode( encoded, nil, true )
assert( s == decoded )
decodetime = os.clock() - t
print('Common text (cache)')
print(('Encoding: %d bytes/sec'):format( math.floor(N/encodetime)))
print(('Decoding: %d bytes/sec'):format( math.floor(N/decodetime)))
collectgarbage()
local lt = {}
for i = 0, 255 do
lt[i] = string.char(i)
end
nletters = #lt
for i = 1, N do
local j = math.random( nletters )
st[i] = lt[j]
end
s = table.concat( st )
t = os.clock()
encoded = base64.encode( s, nil )
encodetime = os.clock() - t
t = os.clock()
decoded = base64.decode( encoded )
decodetime = os.clock() - t
assert( s == decoded )
print('Binary')
print(('Encoding: %d bytes/sec'):format( math.floor(N/encodetime)))
print(('Decoding: %d bytes/sec'):format( math.floor(N/decodetime)))
collectgarbage()
t = os.clock()
encoded = base64.encode( s, nil, true )
encodetime = os.clock() - t
t = os.clock()
decoded = base64.decode( encoded, nil, true )
assert( s == decoded )
decodetime = os.clock() - t
print('Binary (cache)')
print(('Encoding: %d bytes/sec'):format( math.floor(N/encodetime)))
print(('Decoding: %d bytes/sec'):format( math.floor(N/decodetime)))
collectgarbage()