Skip to content

Commit

Permalink
Add a few simple apples-to-apples formatting benchmarks.
Browse files Browse the repository at this point in the history
--HG--
rename : tests/Benchmarks.hs => benchmarks/Benchmarks.hs
rename : tests/Makefile => benchmarks/Makefile
  • Loading branch information
bos committed Jun 5, 2011
1 parent 923c2de commit 6222f99
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .hgignore
@@ -1,5 +1,5 @@
^(?:cabal-dev|dist)$
^tests/bm$
^benchmarks/(?:bm|simple|sprintf|swprintf)$
\.(?:aux|eventlog|h[ip]|log|[oa]|orig|prof|ps|rej|swp)$
~$
syntax: glob
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions benchmarks/Makefile
@@ -0,0 +1,20 @@
ghc := ghc

programs := bm simple sprintf swprintf

all: $(programs)

bm: Benchmarks.hs
$(ghc) -rtsopts -O -o $@ $<

simple: Simple.hs
$(ghc) -rtsopts -O -o $@ $<

sprintf: sprintf.c
$(CC) -O2 -o $@ $<

swprintf: swprintf.c
$(CC) -O2 -o $@ $<

clean:
-rm -f $(programs) *.hi *.o *.hp
20 changes: 20 additions & 0 deletions benchmarks/Simple.hs
@@ -0,0 +1,20 @@
{-# LANGUAGE BangPatterns, OverloadedStrings #-}

import Control.Monad
import System.Environment
import qualified Data.Text.Format as T
import Data.Time.Clock

main = do
args <- getArgs
let count = case args of
(x:_) -> read x :: Int
_ -> 100000
start <- getCurrentTime
forM_ [0..count] $ \i -> do
let !t = T.format "hi mom {}\n" [fromIntegral i * pi::Double]
return ()
elapsed <- (`diffUTCTime` start) `fmap` getCurrentTime
T.print "{} iterations in {} secs ({} thousand/sec\n"
(count, elapsed,
fromRational (toRational count / toRational elapsed / 1e3) :: Double)
43 changes: 43 additions & 0 deletions benchmarks/sprintf.c
@@ -0,0 +1,43 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

double gettime(void)
{
struct timeval tv;

gettimeofday(&tv, NULL);

return tv.tv_sec + (tv.tv_usec / 1e6);
}

void loop(int count)
{
int i;

for (i = 0; i < count; i++) {
char *s = malloc(64);

sprintf(s, "hi mom %g\n", (double) i * M_PI);

free(s);
}
}

int main(int argc, char **argv)
{
double start, elapsed;
int i, count;

count = argc == 2 ? atoi(argv[1]) : 1600000;

start = gettime();

loop(count);

elapsed = gettime() - start;

printf("%d iterations in %g secs (%g thousand/sec)\n", count, elapsed,
count / elapsed / 1e3);
}
44 changes: 44 additions & 0 deletions benchmarks/swprintf.c
@@ -0,0 +1,44 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <wchar.h>

double gettime(void)
{
struct timeval tv;

gettimeofday(&tv, NULL);

return tv.tv_sec + (tv.tv_usec / 1e6);
}

void loop(int count)
{
int i;

for (i = 0; i < count; i++) {
wchar_t *s = malloc(64 * sizeof(wchar_t));

swprintf(s, 64, L"hi mom %g\n", (double) i * M_PI);

free(s);
}
}

int main(int argc, char **argv)
{
double start, elapsed;
int i, count;

count = argc == 2 ? atoi(argv[1]) : 1600000;

start = gettime();

loop(count);

elapsed = gettime() - start;

printf("%d iterations in %g secs (%g thousand/sec)\n", count, elapsed,
count / elapsed / 1e3);
}
9 changes: 0 additions & 9 deletions tests/Makefile

This file was deleted.

0 comments on commit 6222f99

Please sign in to comment.