github.com/aergoio/aergo@v1.3.1/contract/measure/bf.lua (about) 1 loop_cnt = 1000 2 arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } 3 tbl = { name= "kslee", year= 1981, age= 41 } 4 long_str = [[Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well. 5 ]] 6 long_str1 = [[Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as werr. 7 ]] 8 9 local print = function(...) 10 system.print(...) 11 end 12 13 function copy_arr(arr) 14 local a = {} 15 for i, v in ipairs(arr) do 16 a[i] = v 17 end 18 return a 19 end 20 21 function measure(fn, ...) 22 local start = nsec() 23 fn(...) 24 return nsec() - start 25 end 26 27 function m1k(fn, ...) 28 local r1k = function(fn) 29 return function(...) 30 for i = 1, loop_cnt do 31 fn(...) 32 end 33 end 34 end 35 t = measure(r1k(fn), ...) 36 print(string.format('%.9f', t / loop_cnt)) 37 end 38 39 function print_chain(fn) 40 return function(...) 41 print(fn(...)) 42 end 43 end 44 45 function m1kp(fn, ...) 46 m1k(print_chain(fn), ...) 47 end 48 49 function basic_fns() 50 print("assert") 51 m1k(assert, true, 'true') 52 m1k(assert, 1 == 1, 'true') 53 m1k(assert, 1 ~= 2, 'true') 54 m1k(assert, long_str == long_str, 'true') 55 m1k(assert, long_str ~= long_str1, 'true') 56 print("getfenv") 57 m1k(getfenv, basic_fns) 58 local x = {value = 5} 59 local mt = { 60 __add = function (lhs, rhs) 61 return { value = lhs.value + rhs.value } 62 end, 63 __tostring = function (self) 64 return "Hello Aergo" 65 end 66 } 67 setmetatable(x, mt) 68 print("getmetable") 69 m1k(getmetatable, x) 70 print("ipairs") 71 m1k(ipairs, arr) 72 print("next") 73 m1k(next, tbl) 74 m1k(next, tbl, "year") 75 m1k(next, tbl, "name") 76 print("pairs") 77 m1k(pairs, tbl) 78 print("rawequal") 79 m1k(rawequal, 1, 1) 80 m1k(rawequal, 1, 2) 81 m1k(rawequal, 1.4, 2.1) 82 m1k(rawequal, 1.4, 2) 83 m1k(rawequal, "hello", "world") 84 m1k(rawequal, arr, tbl) 85 m1k(rawequal, arr, arr) 86 m1k(rawequal, tbl, tbl) 87 print("rawget") 88 local a = arr 89 m1k(rawget, a, 1) 90 m1k(rawget, a, 10) 91 m1k(rawget, tbl, "age") 92 print("rawset") 93 local a = copy_arr(arr) 94 m1k(rawset, a, 1, 0) 95 m1k(rawset, a, 10, 1) 96 m1k(rawset, a, 11, -1) 97 m1k(rawset, tbl, "addr", "aergo") 98 m1k(rawset, tbl, "age", 42) 99 print("select") 100 m1k(select, '#', 'a', 'b', 'c', 'd') 101 m1k(select, '#', arr) 102 m1k(select, '2', 'a', 'b', 'c', 'd') 103 m1k(select, '2', arr) 104 m1k(select, '6', arr) 105 m1k(select, '9', arr) 106 print("setfenv") 107 fenv = getfenv(basic_fns) 108 m1k(setfenv, basic_fns, fenv) 109 print("setmetatable") 110 m1k(setmetatable, x, mt) 111 print("tonumber") 112 m1k(tonumber, 0x10, 16) 113 m1k(tonumber, '112134', 16) 114 m1k(tonumber, '112134') 115 m1k(tonumber, 112134) 116 print("tostring") 117 m1k(tostring, 'i am a string') 118 m1k(tostring, 1) 119 m1k(tostring, 112134) 120 m1k(tostring, true) 121 m1k(tostring, nil) 122 m1k(tostring, 3.14) 123 m1k(tostring, 3.14159267) 124 m1k(tostring, x) 125 print("type") 126 m1k(type, '112134') 127 m1k(type, 112134) 128 m1k(type, {}) 129 m1k(type, type) 130 print("unpack") 131 m1k(unpack, {1, 2, 3, 4, 5}, 2, 4) 132 m1k(unpack, arr, 2, 4) 133 m1k(unpack, {1, 2, 3, 4, 5}) 134 m1k(unpack, arr) 135 local a = {} 136 for i = 1, 100 do 137 a[i] = i * i 138 end 139 m1k(unpack, a, 2, 4) 140 m1k(unpack, a, 10, 40) 141 m1k(unpack, a) 142 end 143 144 function string_fns() 145 print("string.byte") 146 m1k(string.byte, "hello string", 3, 7) 147 m1k(string.byte, long_str, 3, 7) 148 m1k(string.byte, long_str, 1, #long_str) 149 print("string.char") 150 m1k(string.char, 72, 101, 108, 108, 111) 151 m1k(string.char, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100) 152 m1k(string.char, string.byte(long_str, 1, #long_str)) 153 print("string.dump") 154 m1k(string.dump, m1k) 155 m1k(string.dump, basic_fns) 156 print("string.find") 157 m1k(string.find, long_str, "nume.....") 158 m1k(string.find, long_str, "we..") 159 m1k(string.find, long_str, "wi..") 160 m1k(string.find, long_str, "pattern") 161 m1k(string.find, long_str, "pattern", 200) 162 m1k(string.find, "hello world hellow", "hello") 163 m1k(string.find, "hello world hellow", "hello", 3) 164 m1k(string.find, long_str, "head") 165 print("string.format") 166 m1k(string.format, "string.format %d, %.9f, %e, %g: %s", 1, 1.999999999, 10e9, 3.14, "end of string") 167 m1k(string.format, "string.format %d, %.9f, %e", 1, 1.999999999, 10e9) 168 print("stirng.gmatch") 169 s = "hello world from Lua" 170 m1k(string.gmatch, s, "%a+") 171 m1k(function() 172 for w in string.gmatch(s, "%a+") do 173 end 174 end) 175 print("string.gsub") 176 m1k(string.gsub, s, "(%w+)", "%1 %1") 177 s2 = s .. ' ' .. s 178 m1k(string.gsub, s2, "(%w+)", "%1 %1") 179 m1k(string.gsub, s, "(%w+)%s*(%w+)", "%2 %1") 180 print("string.len") 181 m1k(string.len, s) 182 m1k(string.len, s2) 183 m1k(string.len, long_str) 184 print("string.lower") 185 m1k(string.lower, s) 186 m1k(string.lower, long_str) 187 print("string.match") 188 m1k(string.match, s, "(L%w+)") 189 print("string.rep") 190 m1k(string.rep, s, 2) 191 m1k(string.rep, s, 4) 192 m1k(string.rep, s, 8) 193 m1k(string.rep, s, 16) 194 m1k(string.rep, long_str, 16) 195 print("string.reverse") 196 m1k(string.reverse, s) 197 m1k(string.reverse, s2) 198 m1k(string.reverse, long_str) 199 print("string.sub") 200 m1k(string.sub, s, 10, 13) 201 m1k(string.sub, s, 10, -3) 202 m1k(string.sub, long_str, 10, 13) 203 m1k(string.sub, long_str, 10, -3) 204 print("string.upper") 205 m1k(string.upper, s) 206 m1k(string.upper, s2) 207 m1k(string.upper, long_str) 208 end 209 210 function table_fns1() 211 local a = copy_arr(arr) 212 local a100 = {} 213 for i = 1, 100 do 214 a100[i] = i * i 215 end 216 print("table.concat") 217 m1k(table.concat, a, ',') 218 m1k(table.concat, a, ',', 3, 7) 219 m1k(table.concat, a100, ',') 220 m1k(table.concat, a100, ',', 3, 7) 221 m1k(table.concat, a100, ',', 3, 32) 222 local as10 = {} 223 for i = 1, 10 do 224 as10[i] = "hello" 225 end 226 local as100 = {} 227 for i = 1, 100 do 228 as100[i] = "hello" 229 end 230 m1k(table.concat, as10, ',') 231 m1k(table.concat, as10, ',', 3, 7) 232 m1k(table.concat, as100, ',') 233 m1k(table.concat, as100, ',', 3, 7) 234 m1k(table.concat, as100, ',', 3, 32) 235 for i = 1, 10 do 236 as10[i] = "h" 237 end 238 for i = 1, 100 do 239 as100[i] = "h" 240 end 241 m1k(table.concat, as10, ',') 242 m1k(table.concat, as10, ',', 3, 7) 243 m1k(table.concat, as100, ',') 244 m1k(table.concat, as100, ',', 3, 7) 245 m1k(table.concat, as100, ',', 3, 32) 246 end 247 248 function table_fns2() 249 local a = copy_arr(arr) 250 local a100 = {} 251 for i = 1, 100 do 252 a100[i] = i * i 253 end 254 print("table.insert") 255 m1k(table.insert, a, 11) 256 for i = 1, 1000 do 257 table.remove(a) 258 end 259 m1k(table.insert, a, 5, 11) 260 for i = 1, 1000 do 261 table.remove(a, 5) 262 end 263 --m1k(table.insert, a, 5, -5) 264 --m1k(table.insert, a, 1, -5) 265 m1k(table.insert, a100, 11) 266 for i = 1, 1000 do 267 table.remove(a100) 268 end 269 m1k(table.insert, a100, 5, -5) 270 for i = 1, 1000 do 271 table.remove(a100, 5) 272 end 273 --m1k(table.insert, a100, 1, -5) 274 end 275 276 function table_fns3() 277 local a = copy_arr(arr) 278 local a100 = {} 279 for i = 1, 100 do 280 a100[i] = i * i 281 end 282 print("table.maxn") 283 m1k(table.maxn, a) 284 m1k(table.maxn, a100) 285 print("table.remove") 286 for i = 1, 1000 do 287 table.insert(a, i) 288 end 289 m1k(table.remove, a) 290 for i = 1, 1000 do 291 table.insert(a, 5, i) 292 end 293 m1k(table.remove, a, 5) 294 for i = 1, 1000 do 295 table.insert(a100, i) 296 end 297 m1k(table.remove, a100) 298 for i = 1, 1000 do 299 table.insert(a100, 5, i) 300 end 301 m1k(table.remove, a100, 5) 302 end 303 304 function table_fns4() 305 local a = copy_arr(arr) 306 local a100 = {} 307 for i = 1, 100 do 308 a100[i] = i * i 309 end 310 print("table.sort") 311 m1k(table.sort, a) 312 m1k(table.sort, a, function(x,y) return x > y end) 313 m1k(table.sort, a100) 314 m1k(table.sort, a100, function(x,y) return x > y end) 315 end 316 317 function math_fns() 318 d = {} 319 for i = 1, loop_cnt do 320 d[i] = -500 + i 321 end 322 for i = 1, loop_cnt, 10 do 323 d[i] = d[i] + 0.5 324 end 325 for i = 1, loop_cnt, 13 do 326 d[i] = d[i] + 0.3 327 end 328 for i = 1, loop_cnt, 17 do 329 d[i] = d[i] + 0.7 330 end 331 f = {} 332 for i = 1, loop_cnt do 333 f[i] = -1 + i * 0.002 334 end 335 local md = function (fn, d, ...) 336 local x = function (fn, d) 337 return function(...) 338 for i, v in ipairs(d) do 339 fn(v, ...) 340 end 341 end 342 end 343 t = measure(x(fn, d), ...) 344 print(string.format('%.9f', t / #d)) 345 end 346 print("math.abs") 347 md(math.abs, d) 348 print("math.acos") 349 md(math.acos, f) 350 print("math.asin") 351 md(math.asin, f) 352 print("math.atan") 353 md(math.atan, f) 354 print("math.atan2") 355 md(math.atan2, f, 2) 356 print("math.ceil") 357 md(math.ceil, f) 358 print("math.cos") 359 md(math.cos, d) 360 md(math.cos, f) 361 print("math.cosh") 362 md(math.cosh, d) 363 md(math.cosh, f) 364 print("math.deg") 365 md(math.deg, f) 366 print("math.exp") 367 md(math.exp, d) 368 md(math.exp, f) 369 print("math.floor") 370 md(math.floor, d) 371 md(math.floor, f) 372 print("math.fmod") 373 md(math.fmod, f, 1.4) 374 print("math.frexp") 375 md(math.frexp, d) 376 md(math.frexp, f) 377 print("math.log") 378 local filter = function(l, cond) 379 r = {} 380 for i, v in ipairs(l) do 381 if cond(v) then 382 table.insert(r, v) 383 end 384 end 385 return r 386 end 387 ud = filter(d, function(v) return v >= 0 end) 388 uf = filter(f, function(v) return v >= 0 end) 389 md(math.log, ud) 390 md(math.log, uf) 391 print("math.log10") 392 md(math.log10, ud) 393 md(math.log10, uf) 394 print("math.max") 395 m1k(math.max, unpack(ud)) 396 m1k(math.max, unpack(d)) 397 m1k(math.max, unpack(uf)) 398 m1k(math.max, unpack(f)) 399 print("math.min") 400 m1k(math.min, unpack(ud)) 401 m1k(math.min, unpack(d)) 402 m1k(math.min, unpack(uf)) 403 m1k(math.min, unpack(f)) 404 print("math.modf") 405 md(math.modf, d) 406 md(math.modf, f) 407 print("math.pow") 408 md(math.pow, d, 2) 409 md(math.pow, d, 4) 410 md(math.pow, d, 8) 411 md(math.pow, f, 2) 412 md(math.pow, f, 4) 413 md(math.pow, f, 8) 414 md(math.pow, ud, 8.4) 415 md(math.pow, uf, 8.4) 416 print("math.sin") 417 md(math.sin, d) 418 md(math.sin, f) 419 print("math.sinh") 420 md(math.sinh, d) 421 md(math.sinh, f) 422 print("math.sqrt") 423 md(math.sqrt, ud) 424 md(math.sqrt, uf) 425 print("math.tan") 426 md(math.tan, d) 427 md(math.tan, f) 428 print("math.tanh") 429 md(math.tanh, f) 430 end 431 432 function bit_fns() 433 local printx = function (fn) 434 return function(...) 435 print("0x"..bit.tohex(fn(...))) 436 end 437 end 438 print("bit.tobit") 439 m1k(bit.tobit, 0xffffffff) 440 m1k(bit.tobit, 0xffffffff + 1) 441 m1k(bit.tobit, 2^40 + 1234) 442 print("bit.tohex") 443 m1k(bit.tohex, 1) 444 m1k(bit.tohex, -1) 445 m1k(bit.tohex, -1, -8) 446 m1k(bit.tohex, 0x87654321, 4) 447 print("bit.bnot") 448 m1k(bit.bnot, 0) 449 m1k(bit.bnot, 0x12345678) 450 print("bit.bor") 451 m1k(bit.bor, 1) 452 m1k(bit.bor, 1, 2) 453 m1k(bit.bor, 1, 2, 4) 454 m1k(bit.bor, 1, 2, 4, 8) 455 print("bit.band") 456 m1k(bit.band, 0x12345678, 0xff) 457 m1k(bit.band, 0x12345678, 0xff, 0x3f) 458 m1k(bit.band, 0x12345678, 0xff, 0x3f, 0x1f) 459 print("bit.xor") 460 m1k(bit.bxor, 0xa5a5f0f0, 0xaa55ff00) 461 m1k(bit.bxor, 0xa5a5f0f0, 0xaa55ff00, 0x18000000) 462 m1k(bit.bxor, 0xa5a5f0f0, 0xaa55ff00, 0x18000000, 0x00000033) 463 print("bit.lshift") 464 m1k(bit.lshift, 1, 0) 465 m1k(bit.lshift, 1, 8) 466 m1k(bit.lshift, 1, 40) 467 print("bit.rshift") 468 m1k(bit.rshift, 256, 0) 469 m1k(bit.rshift, 256, 8) 470 m1k(bit.rshift, 256, 40) 471 print("bit.ashift") 472 m1k(bit.arshift, 0x87654321, 0) 473 m1k(bit.arshift, 0x87654321, 12) 474 m1k(bit.arshift, 0x87654321, 40) 475 print("bit.rol") 476 m1k(bit.rol, 0x12345678, 0) 477 m1k(bit.rol, 0x12345678, 12) 478 m1k(bit.rol, 0x12345678, 40) 479 print("bit.ror") 480 m1k(bit.ror, 0x12345678, 0) 481 m1k(bit.ror, 0x12345678, 12) 482 m1k(bit.ror, 0x12345678, 40) 483 print("bit.bswap") 484 m1k(bit.bswap, 0x12345678) 485 end 486 487 abi.register(basic_fns,string_fns,table_fns1,table_fns2,table_fns3,table_fns4,math_fns,bit_fns)