Skip to content

Instantly share code, notes, and snippets.

@eriknw
Last active January 3, 2016 12:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriknw/8466278 to your computer and use it in GitHub Desktop.
Save eriknw/8466278 to your computer and use it in GitHub Desktop.
Additional Python and Lua benchmarks for: http://matthewrocklin.com/blog/work/2014/01/13/Text-Benchmarks/
local function groupby(func, seq)
local t = {}
for _, val in ipairs(seq) do
local key = func(val)
t[key] = t[key] or {}
t[key][#t[key] + 1] = val
end
return t
end
local function dowork(filename)
local f = io.open(filename, "r")
local t = {}
for n1, n2 in string.gmatch(f:read("*all"), "(%S+) (%S+)") do
t[#t + 1] = {n1, n2}
end
f:close()
return groupby(function(x) return x[1] end, t)
end
return { dowork=dowork }
from sys import argv
from toolz import groupby, first
from utils import duration
from re import findall
from operator import itemgetter
filename = argv[1]
with duration():
with open(filename, 'r') as file:
result = groupby(itemgetter(0), findall('(\S+) (\S+)', file.read()))
-- instead of calling `groupby`, we store the word pairs as done in the Java example
local function dowork(filename)
local f = io.open(filename, "r")
local t = {}
for k, v in string.gmatch(f:read("*all"), "(%S+) (%S+)") do
t[k] = t[k] or {}
t[k][#t[k] + 1] = v
end
f:close()
return t
end
return { dowork=dowork }
# these are the commands I used to install `lupa` on 64-bit Ubuntu
git clone https://github.com/scoder/lupa.git
cd lupa
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar zxvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make CFLAGS="-fPIC"
cd ../
# add "--user" to below to install only for current user
# add "--with-cython" if you want to build lupa using cython
python setup.py install
import lupa
from sys import argv
from utils import duration
lua = lupa.LuaRuntime()
t = lua.require('benchmark1')
filename = argv[1]
with duration():
t.dowork(filename)
import threading
import lupa
from sys import argv
from utils import duration
thread_count = len(argv) - 1
lua_funcs = [lupa.LuaRuntime().require('benchmark1').dowork
for _ in range(thread_count)]
results = [None] * thread_count
def runthread(i, filename, lua_func):
results[i] = lua_func(filename)
threads = [threading.Thread(target=runthread, args=(i, argv[i+1], lua_func))
for i, lua_func in enumerate(lua_funcs)]
with duration():
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment