Created
August 23, 2012 03:22
-
-
Save dansimau/3431838 to your computer and use it in GitHub Desktop.
Testing performance of md5 hash calculation in lua vs node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
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
Results
lua /
md5.hexa()
:luajit /
md5.hexa()
:lua /
md5.sum()
:luajit /
md5.sum()
:node (V8):
Comparison
For 1000000 iterations:
hexa()
hexa()