-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhamming.pir
90 lines (69 loc) · 1.65 KB
/
hamming.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright (C) 2009, Parrot Foundation.
=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: