[svn:parrot] r42921 - in trunk: . examples/benchmarks

dukeleto at svn.parrot.org dukeleto at svn.parrot.org
Mon Dec 7 06:09:39 UTC 2009


Author: dukeleto
Date: Mon Dec  7 06:09:36 2009
New Revision: 42921
URL: https://trac.parrot.org/parrot/changeset/42921

Log:
[examples] Add a benchmark geared towards strings which computes the Hamming distance between two strings

Added:
   trunk/examples/benchmarks/hamming.pir   (contents, props changed)
Modified:
   trunk/MANIFEST

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST	Sun Dec  6 18:31:54 2009	(r42920)
+++ trunk/MANIFEST	Mon Dec  7 06:09:36 2009	(r42921)
@@ -1,7 +1,7 @@
 # ex: set ro:
 # $Id$
 #
-# generated by tools/dev/mk_manifest_and_skip.pl Sun Dec  6 07:04:29 2009 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Mon Dec  7 05:44:40 2009 UT
 #
 # See below for documentation on the format of this file.
 #
@@ -586,6 +586,7 @@
 examples/benchmarks/gc_waves_headers.pasm                   [examples]
 examples/benchmarks/gc_waves_sizeable_data.pasm             [examples]
 examples/benchmarks/gc_waves_sizeable_headers.pasm          [examples]
+examples/benchmarks/hamming.pir                             [examples]
 examples/benchmarks/hello.pir                               [examples]
 examples/benchmarks/mops.pasm                               [examples]
 examples/benchmarks/mops.pl                                 [examples]

Added: trunk/examples/benchmarks/hamming.pir
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/examples/benchmarks/hamming.pir	Mon Dec  7 06:09:36 2009	(r42921)
@@ -0,0 +1,91 @@
+# Copyright (C) 2009, Parrot Foundation.
+# $Id$
+
+=head1 NAME
+
+examples/benchmarks/hamming.pir - calculate hamming distance between two strings
+
+=head1 SYNOPSIS
+
+    ./parrot examples/benchmarks/hamming.pir foobar foozibar
+
+=head1 DESCRIPTION
+
+Calculate the number of characters that are different between two strings.
+Strings need not be the same length. This benchmark should be useful for
+looking into the performance of String PMC -> string conversion and function calls.
+
+=cut
+
+.sub main
+    .param pmc argv
+    .local pmc s1, s2
+    .local int argc
+    $S0  = shift argv  # get rid of filename
+    argc = argv
+
+    s1 = new 'String'
+    s2 = new 'String'
+
+    if argc == 2 goto get_args
+
+    s1 = "bbbcdebbbcdebbcdebcdbcdebbcdebebbcdebcdebbcdebbbcdebbcdebbcdebbcdebcdef"
+    s2 = "acdbcdeabcdeaeaabcdeabbcdeadeaeabcdebcdeabcdeaabcdeabcdeabcdeabcdebcdef"
+    goto get_distance
+  get_args:
+    s1 = argv[0]
+    s2 = argv[1]
+  get_distance:
+    $I0 = distance(s1,s2)
+    print $I0
+    print "\n"
+.end
+
+.sub distance
+    .param string s1
+    .param string s2
+    .local int dist
+    .local int min, max
+    dist = 0
+
+    $I0  = length s1
+    $I1  = length s2
+    min  = $I0
+    max  = $I1
+    if $I0 < $I1 goto calc_dist
+    min = $I1
+    max = $I0
+
+  calc_dist:
+    dist = max - min
+
+    .local int k
+    k = 0
+  loop:
+    $S1 = get_char(s1,k)
+    $S2 = get_char(s2,k)
+
+
+    $I4 = $S1 != $S2
+
+    dist += $I4
+    inc k
+    if  k >= min goto done
+    goto loop
+  done:
+    .return (dist)
+.end
+
+.sub get_char
+    .param string s
+    .param int k
+    $S0 = substr s, k, 1
+    .return ($S0)
+.end
+
+# Local Variables:
+#   mode: pir
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4 ft=pir:
+


More information about the parrot-commits mailing list