Permalink
Browse files

Add a few simple apples-to-apples formatting benchmarks.

--HG--
rename : tests/Benchmarks.hs => benchmarks/Benchmarks.hs
rename : tests/Makefile => benchmarks/Makefile
  • Loading branch information...
1 parent 923c2de commit 6222f99c0132efcfacfa9964d771df53d3a1ae5d @bos committed Jun 5, 2011
Showing with 128 additions and 10 deletions.
  1. +1 −1 .hgignore
  2. 0 {tests → benchmarks}/Benchmarks.hs
  3. +20 −0 benchmarks/Makefile
  4. +20 −0 benchmarks/Simple.hs
  5. +43 −0 benchmarks/sprintf.c
  6. +44 −0 benchmarks/swprintf.c
  7. +0 −9 tests/Makefile
View
2 .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
View
0 tests/Benchmarks.hs → benchmarks/Benchmarks.hs
File renamed without changes.
View
20 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
View
20 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)
View
43 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);
+}
View
44 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);
+}
View
9 tests/Makefile
@@ -1,9 +0,0 @@
-ghc := ghc
-
-all: bm
-
-bm: Benchmarks.hs
- $(ghc) -rtsopts -O -o $@ $<
-
-clean:
- -rm -f bm *.hi *.o *.hp

0 comments on commit 6222f99

Please sign in to comment.