Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created August 23, 2012 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansimau/3431838 to your computer and use it in GitHub Desktop.
Save dansimau/3431838 to your computer and use it in GitHub Desktop.
Testing performance of md5 hash calculation in lua vs node
require("md5")
local testname = "lua md5 sum hash test (binary)"
function test()
-- Create object hash
local object_key_plain = [[
"0.0.0.0",
80,
"www.example.com",
"GET",
"/foo/bar"
]]
local object_key = md5.sum(object_key_plain)
end
local testiterations = {100, 1000, 10000, 100000, 1000000}
io.write("\n---\nTest: " .. testname .. "\n---\n\n");
for t=1,#testiterations do
-- Start time
local starttime = os.clock()
-- Run test
for i=0,testiterations[t] do
test()
end
-- End time
local endtime = os.clock()
local totaltime = (endtime - starttime) * 1000
-- Print results
io.write("Iterations: " .. testiterations[t] .. "; Time: " .. totaltime .. " ms\n");
end
require("md5")
local testname = "lua md5 sum hash test (hex)"
function test()
-- Create object hash
local object_key_plain = [[
"0.0.0.0",
80,
"www.example.com",
"GET",
"/foo/bar"
]]
local object_key = md5.sumhexa(object_key_plain)
end
local testiterations = {100, 1000, 10000, 100000, 1000000}
io.write("\n---\nTest: " .. testname .. "\n---\n\n");
for t=1,#testiterations do
-- Start time
local starttime = os.clock()
-- Run test
for i=0,testiterations[t] do
test()
end
-- End time
local endtime = os.clock()
local totaltime = (endtime - starttime) * 1000
-- Print results
io.write("Iterations: " .. testiterations[t] .. "; Time: " .. totaltime .. " ms\n");
end
var console = require("console");
var crypto = require("crypto");
var testName = "JavaScript md5sum hash test";
// Code to test
function test() {
// Create object hash
var object_key_plain = [
"0.0.0.0",
80,
"www.example.com",
"GET",
"/foo/bar"
];
var object_key = crypto.createHash('md5').update(JSON.stringify(object_key_plain)).digest('hex');
}
var testIterations = new Array(100, 1000, 10000, 100000, 1000000);
console.log("\n---\nTest: " + testName + "\n---\n");
for (var t=0, l=testIterations.length; t<l; t++) {
// Start time
var startTime = (new Date()).getTime();
// Test init
var testDate = new Date();
// Run test
for (var i=0; i<testIterations[t]; i++) {
test();
}
// End time
var endTime = (new Date()).getTime();
var totalTime = (endTime - startTime);
// Print results
console.log("Iterations: " + testIterations[t] + "; Time: " + totalTime + " ms");
}
@dansimau
Copy link
Author

Results

lua / md5.hexa():

$ lua ./md5-test-hex.lua


---
Test: lua md5 sum hash test (hex)

---

Iterations: 100; Time: 2.088 ms
Iterations: 1000; Time: 12.742 ms
Iterations: 10000; Time: 140.823 ms
Iterations: 100000; Time: 1253.17 ms
Iterations: 1000000; Time: 12385.991 ms

luajit / md5.hexa():

$ luajit ./md5-test-hex.lua


---
Test: lua md5 sum hash test (hex)

---

Iterations: 100; Time: 1.177 ms
Iterations: 1000; Time: 8.917 ms
Iterations: 10000; Time: 75.312 ms
Iterations: 100000; Time: 685.589 ms
Iterations: 1000000; Time: 7024.637 ms

lua / md5.sum():

$ lua ./md5-test-bin.lua


---
Test: lua md5 sum hash test (binary)

---

Iterations: 100; Time: 0.132 ms
Iterations: 1000; Time: 1.235 ms
Iterations: 10000; Time: 12.319 ms
Iterations: 100000; Time: 106.361 ms
Iterations: 1000000; Time: 861.348 ms

luajit / md5.sum():

$ luajit ./md5-test-bin.lua


---
Test: lua md5 sum hash test (binary)

---

Iterations: 100; Time: 0.219 ms
Iterations: 1000; Time: 1.321 ms
Iterations: 10000; Time: 8.856 ms
Iterations: 100000; Time: 80.515 ms
Iterations: 1000000; Time: 711.545 ms

node (V8):

$ node ./md5-test.js


---
Test: javascript md5sum hash test

---

Iterations: 100; Time: 1 ms
Iterations: 1000; Time: 8 ms
Iterations: 10000; Time: 92 ms
Iterations: 100000; Time: 624 ms
Iterations: 1000000; Time: 6187 ms

Comparison

For 1000000 iterations:

lua hexa()luajit hexa()Node
12386 ms7024 ms6187 ms

@dansimau
Copy link
Author

Updated result using luajit and md5 fork from https://github.com/starwing/md5 (thanks @pintsized):


---
Test: lua md5 sum hash test (sumhexa)

---

Iterations: 100; Time: 0.754 ms
Iterations: 1000; Time: 4.966 ms
Iterations: 10000; Time: 35.441 ms
Iterations: 100000; Time: 331.645 ms
Iterations: 1000000; Time: 3405.509 ms

Yay! lua wins!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment