Skip to content

Commit

Permalink
[benchmarks] word_freq example
Browse files Browse the repository at this point in the history
Tickled the bash assoc array bug that Koichi worked around!  Not sure if
this is version-specific.
  • Loading branch information
Andy Chu committed Jul 27, 2020
1 parent 6dc0cee commit 288661d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
36 changes: 36 additions & 0 deletions benchmarks/compute/word_freq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python2
"""
word_freq.py
"""
from __future__ import print_function

import sys


def main(argv):
try:
iters = int(argv[1])
except IndexError:
iters = 1000

text = sys.stdin.read()

words = {}

for i in xrange(iters):
for word in text.split():
if word in words:
words[word] += 1
else:
words[word] = 1

for word in words:
print("%d %s" % (words[word], word))


if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)
35 changes: 35 additions & 0 deletions benchmarks/compute/word_freq.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

main() {
iters=${1:-1000}

# read it once
read -d '' text

declare -A words

# do it a bunch of times
for (( i = 0; i < iters; ++i )); do
for word in $text; do # relies on word splitting

# Hm this isn't correct in bash!
#(( words["$word"] += 1 ))

# This seems to work? wtf?
# This causes a parse error in OSH though... Do we need two benchmarks?

# Or maybe we need something to turn of static parsing?
# similar to git

(( words[\$word] += 1 ))
done
done

# note: we can sort the output in the benchmark and assert that it's the same?

for word in "${!words[@]}"; do
echo "${words["$word"]} $word"
done
}

main "$@"

0 comments on commit 288661d

Please sign in to comment.