Skip to content

Instantly share code, notes, and snippets.

@askagirl
Forked from ageldama/SillyMapBench.hs
Created October 25, 2015 07:34
Show Gist options
  • Save askagirl/b4cdb7c9ce3b2770656d to your computer and use it in GitHub Desktop.
Save askagirl/b4cdb7c9ce3b2770656d to your computer and use it in GitHub Desktop.
n_times(1000000).
texts(["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog", "at", "a", "restaurant", "near", "the", "lake", "of", "a", "new", "era"]).
factorial(N, R) :- factorial(N, 1, R).
factorial(0, R, R) :- !.
factorial(N, Acc, R) :-
NewN is N - 1,
NewAcc is Acc * N,
factorial(NewN, NewAcc, R).
% time((factorial(99999, _), true)).
n_loop(0) :- !.
n_loop(N) :- write(N), nl,
N2 is N - 1, n_loop(N2).
% iter over list?
write_nl(X) :- write(X), nl.
% maplist(write_nl, [1, 2, 3]).
iter_over_list([]) :- !.
iter_over_list([Hd|Tl]) :- write_nl(Hd), iter_over_list(Tl).
% assoc?
% empty_assoc(Assoc), put_assoc(apple, Assoc, fruit, Assoc2), get_assoc(apple, Assoc2, X).
inc_or_1_assoc(Assoc, Key, Assoc2) :- (get_assoc(Key, Assoc, X), X2 is X + 1, put_assoc(Key, Assoc, X2, Assoc2), !);
(put_assoc(Key, Assoc, 1, Assoc2)).
% empty_assoc(X), inc_or_1_assoc(X, apple, Y), inc_or_1_assoc(Y, apple, Z), inc_or_1_assoc(Z, banana, Q), get_assoc(apple, Q, A).
inc_by_text([], AssocResult, AssocResult) :- !.
inc_by_text([Text|Tl], AssocResult, Assoc) :- inc_or_1_assoc(Assoc, Text, Assoc2),
inc_by_text(Tl, AssocResult, Assoc2).
inc_by_text_n_times(0, AssocResult, AssocResult) :- !.
inc_by_text_n_times(N, AssocResult, Assoc) :- texts(Texts), inc_by_text(Texts, Assoc2, Assoc),
N2 is N - 1,
inc_by_text_n_times(N2, AssocResult, Assoc2).
% empty_assoc(Assoc), inc_by_text_n_times(10, AssocResult, Assoc).
run_my_bench(AssocResult) :- empty_assoc(Assoc), n_times(N), inc_by_text_n_times(N, AssocResult, Assoc).
run_my_bench_write :- run_my_bench(Assoc), write(Assoc), nl.
run_my_bench_write_time :- time((run_my_bench_write, true)).
run_my_bench_write_time_halt :- time((run_my_bench_write, true)), halt.
% qsave_program(silly_map_bench, [goal(run_my_bench_write_time_halt), stand_alone(true)]).
%% 767,000,081 inferences, 237.858 CPU in 243.075 seconds (98% CPU, 3224617 Lips)
run
| loopCount texts m |
loopCount := 1000000.
texts := #('The' 'quick' 'brown' 'fox' 'jumped' 'over' 'the' 'lazy' 'dog' 'at' 'a' 'restaurant' 'near' 'the' 'lake' 'of' 'a' 'new' 'era').
m := Dictionary new.
loopCount timesRepeat: [
texts do: [ :text |
m at: text ifPresent: [ :v | m at: text put: v + 1 ]
ifAbsent: [ m at: text put: 1 ].
].
].
^m.
Transcript show: (Time millisecondsToRun: [
Transcript show: (SillyMapBench new run); cr. ]); cr.
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :iterate))
(import '(iterate:iter iterate:for iterate:repeat) *package*)
(defun update-incf-or-1 (m k)
(declare (optimize (speed 3))
(inline))
(if (null (gethash k m))
(setf (gethash k m) (the fixnum 1))
(incf (gethash k m))))
(defun my-hash-fun (s)
(declare (optimize (speed 3) (safety 0))
(inline))
(char-int (elt s 0)))
(defun my-hash-fun2 (s)
(declare (optimize (speed 3) (safety 0))
(inline))
(length s))
(defun my-benchmark ()
(declare (optimize (speed 3) (safety 0)))
(let ((m (make-hash-table))
(+texts+ '("The" "quick" "brown" "fox" "jumped" "over"
"the" "lazy" "dog" "at" "a" "restaurant" "near"
"the" "lake" "of" "a" "new" "era"))
(+times+ (the fixnum 1000000)))
;;; inserts
(iter (repeat +times+)
(iter (for text in +texts+)
(update-incf-or-1 m text)))
;;; print.
(iter
(for (k v) in-hashtable m)
(write k :lines nil) (print v))))
(defun run-benchmark-and-time ()
(time (my-benchmark)))
#lang racket
(define (inc-1 n) (+ n 1))
(define (run-benchmark)
(let ([times 1000000]
[texts '("The" "quick" "brown" "fox" "jumped" "over"
"the" "lazy" "dog" "at" "a" "restaurant" "near"
"the" "lake" "of" "a" "new" "era")]
[m (make-hash)])
(for ((i (- times 1)))
(for ((text texts))
(hash-update! m text inc-1 1)))
(for (((k v) m))
(display k)
(display " ")
(display v)
(newline))))
(time (run-benchmark))
let main () =
let texts = ["The"; "quick"; "brown"; "fox"; "jumped"; "over";
"the"; "lazy"; "dog"; "at"; "a"; "restaurant";
"near"; "the"; "lake"; "of"; "a"; "new"; "era"]
and times = 1000000
and m = Hashtbl.create ~random:true 100 (* ~hashable:String.hashable () *)
and inc_entry = fun h k -> if Hashtbl.mem h k then
Hashtbl.replace h k ((Hashtbl.find h k) + 1)
else
Hashtbl.add h k 1
and start_time = Sys.time()
in
begin
for i = 1 to times do
List.iter (fun text -> inc_entry m text) texts
done;
Hashtbl.iter (fun k v -> Printf.printf "%s %d\n" k v) m;
Printf.printf "Exec-time: %f\n" ((Sys.time()) -. start_time)
end;;
main();;
-module(silly_map_benchmark).
-export([run/0]).
loop_count() -> 1000000.
texts() -> [ "The", "quick", "brown", "fox", "jumped",
"over", "the", "lazy", "dog", "at", "a", "restaurant",
"near", "the", "lake", "of", "a", "new", "era" ].
add_1_or_inc_entry(Text, M) ->
maps:put(Text, (maps:get(Text, M, 0) + 1), M).
add_1_or_inc_entries([], M) -> M;
add_1_or_inc_entries([Text|Texts], M) ->
add_1_or_inc_entries(Texts, add_1_or_inc_entry(Text, M)).
add_or_inc_entries(M) ->
add_1_or_inc_entries(texts(), M).
add_or_inc_entries_times(N, M)
when N > 0 -> add_or_inc_entries_times(N - 1, add_or_inc_entries(M));
add_or_inc_entries_times(_, M)
-> M.
dump_m([], _) -> done;
dump_m([K|Tl], M) ->
io:format("~s ~w~n", [K, maps:get(K, M)]),
dump_m(Tl, M).
dump_m(M) -> dump_m(maps:keys(M), M).
run() ->
statistics(runtime),
statistics(wall_clock),
% your code here
dump_m(add_or_inc_entries_times(loop_count(), maps:new())),
io:format("runtime=~p (Total, Delta) // wall_clock=~p (Total, Delta)~n",
[statistics(runtime), statistics(wall_clock)]).
{-# LANG BangPatterns #-}
-- module SillyMapBench (main) where
import qualified Data.Map.Strict as Map
loopCount = 1000000
-- loopCount = 10
texts = [ "The", "quick", "brown", "fox", "jumped",
"over", "the", "lazy", "dog", "at", "a", "restaurant",
"near", "the", "lake", "of", "a", "new", "era" ]
addOrIncEntry text m = if Map.member text m then
Map.update justOnePlus text m
else
Map.insert text 1 m
where justOnePlus = Just . (+1)
addOrIncEntries [] m = m
addOrIncEntries (text:texts) m = addOrIncEntries texts $! addOrIncEntry text m
addOrIncTextEntries m = addOrIncEntries texts m
addOrIncTextEntriesTimes n m
| n > 0 = addOrIncTextEntriesTimes (n - 1) $! addOrIncTextEntries m
| otherwise = m
main = putStrLn $ show $ addOrIncTextEntriesTimes loopCount $ Map.fromList []
-- $ time ./SillyMapBenchGhc +RTS -sstderr -p
-- ./SillyMapBenchGhc +RTS -sstderr 9.74s user 0.04s system 99% cpu 9.796 total
-- $ ghc SillyMapBench.hs -o SillyMapBenchGhc -rtsopts -O2 -funfolding-use-threshold=16 -fexcess-precision -optc-O3 -prof
import java.util.Map;
import java.util.HashMap;
public class SillyMapBench {
public static void main(String[] args) {
final long START = System.currentTimeMillis();
final int TIMES = 1000000;
final String[] TEXTS = {
"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog", "at", "a",
"restaurant", "near", "the", "lake", "of", "a", "new", "era"
};
Map<String, Integer> m = new HashMap<String, Integer>();
//
for (int i = 0 ; i < TIMES ; i ++) {
for (String text : TEXTS) {
if(m.containsKey(text)){
m.put(text, m.get(text) + 1);
}else{
m.put(text, 1);
}
}
}
//
for (String k : m.keySet()){
Integer v = m.get(k);
System.out.println(k + " " + v);
}
final long END = System.currentTimeMillis();
System.out.println(END - START);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment