Commits

Jasper Van der Jeugt  committed 3b8f8ac

More idiomatic python/ruby programs

  • Participants
  • Parent commits 18b543f

Comments (0)

Files changed (16)

File tests/benchmarks/python/case_map.py

-#!/usr/bin/env python
-
-import utils, sys
-
-for f in sys.argv[1:]:
-    t = utils.benchmark(lambda: utils.with_utf8_file(f, lambda c: c.upper()))
-    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/python/cut.py

+#!/usr/bin/env python
+
+import utils, sys, codecs
+
+def cut(filename, l, r):
+    content = open(filename, encoding='utf-8')
+    for line in content:
+        print(line[l:r])
+
+for f in sys.argv[1:]:
+    t = utils.benchmark(lambda: cut(f, 20, 40))
+    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/python/file_read.py

-#!/usr/bin/env python
-
-import utils, sys
-
-for f in sys.argv[1:]:
-    t = utils.benchmark(lambda: utils.with_utf8_file(f, lambda c: len(c)))
-    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/python/sort.py

 #!/usr/bin/env python
 
-import utils, sys
+import utils, sys, codecs
 
-def sort(string):
-    lines = string.splitlines()
+def sort(filename):
+    content = open(filename, encoding='utf-8').read()
+    lines = content.splitlines()
     lines.sort()
-    return '\n'.join(lines)
+    print('\n'.join(lines))
 
 for f in sys.argv[1:]:
-    t = utils.benchmark(lambda: sys.stdout.write(
-                    				utils.with_utf8_file(f,sort).encode('utf-8'))
-                    				)
+    t = utils.benchmark(lambda: sort(f))
     sys.stderr.write('{0}: {1}\n'.format(f, t))
-

File tests/benchmarks/python/strip_brackets.py

-#!/usr/bin/env python
-
-import utils, sys
-
-def strip_brackets(string):
-    d = 0
-    out = ''
-    for c in string:
-        if c == '{' or c == '[': d += 1
-
-        if d > 0:
-            out += ' '
-        else:
-            out += c
-
-        if c == '}' or c == ']': d -= 1
-
-    return out
-
-for f in sys.argv[1:]:
-    t = utils.benchmark(lambda: utils.with_utf8_file(f, strip_brackets))
-    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/python/strip_tags.py

+#!/usr/bin/env python
+
+import utils, sys
+
+def strip_tags(filename):
+    string = open(filename, encoding='utf-8').read()
+
+    d = 0
+    out = []
+
+    for c in string:
+        if c == '<': d += 1
+
+        if d > 0:
+            out += ' '
+        else:
+            out += c
+
+        if c == '>': d -= 1
+
+    print(''.join(out))
+
+for f in sys.argv[1:]:
+    t = utils.benchmark(lambda: strip_tags(f))
+    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/python/utils.py

         sys.stderr.write('Run {0}: {1}\n'.format(i, result))
         total += result
     return total / runs
-
-def with_utf8_file(filename, f):
-    contents = open(filename).read().decode('utf-8')
-    return f(contents)

File tests/benchmarks/python/word_count.py

-#!/usr/bin/env python
-
-import utils, sys
-
-def word_count(string):
-    freqs = {}
-    for w in string.split():
-        w = w.lower()
-        if freqs.get(w):
-            freqs[w] += 1
-        else:
-            freqs[w] = 1
-    return freqs
-
-for f in sys.argv[1:]:
-    t = utils.benchmark(lambda: utils.with_utf8_file(f, word_count))
-    sys.stderr.write('{0}: {1}\n'.format(f, t))

File tests/benchmarks/ruby/case_map.rb

-#!/usr/bin/env ruby
-
-require './utils.rb'
-
-ARGV.each do |f|
-  t = benchmark { with_utf8_file(f) { |c| c.upcase } }
-  STDERR.puts "#{f}: #{t}"
-end

File tests/benchmarks/ruby/cut.rb

+#!/usr/bin/env ruby
+
+require './utils.rb'
+
+def cut(filename, l, r)
+  File.open(filename, 'r:utf-8') do |file|
+    file.each_line do |line|
+      puts line[l, r - l]
+    end
+  end
+end
+
+ARGV.each do |f|
+  t = benchmark { cut(f, 20, 40) }
+  STDERR.puts "#{f}: #{t}"
+end

File tests/benchmarks/ruby/file_read.rb

-#!/usr/bin/env ruby
-
-require './utils.rb'
-
-ARGV.each do |f|
-  t = benchmark { with_utf8_file(f) { |c| c.size } }
-  STDERR.puts "#{f}: #{t}"
-end

File tests/benchmarks/ruby/sort.rb

 
 require './utils.rb'
 
-def sort(str)
-  str.lines.sort.join
+def sort(filename)
+  File.open(filename, 'r:utf-8') do |file|
+    content = file.read
+    puts content.lines.sort.join
+  end
 end
 
 ARGV.each do |f|
-  t = benchmark do
-    with_utf8_file(f) { |c| puts sort(c) }
-  end
+  t = benchmark { sort(f) }
   STDERR.puts "#{f}: #{t}"
 end

File tests/benchmarks/ruby/strip_brackets.rb

-#!/usr/bin/env ruby
-
-require './utils.rb'
-
-def strip_brackets(str)
-  d = 0
-  out = ''
-
-  str.each_char do |c|
-    d += 1 if c == '{' || c == '['
-    out << if d > 0 then ' ' else c end
-    d -= 1 if c == '}' || c == ']'
-  end
-
-  out
-end
-
-ARGV.each do |f|
-  t = benchmark { with_utf8_file(f) { |c| strip_brackets(c) } }
-  STDERR.puts "#{f}: #{t}"
-end

File tests/benchmarks/ruby/strip_tags.rb

+#!/usr/bin/env ruby
+
+require './utils.rb'
+
+def strip_tags(filename)
+  File.open(filename, 'r:utf-8') do |file|
+    str = file.read
+
+    d = 0
+
+    str.each_char do |c|
+      d += 1 if c == '<'
+      putc(if d > 0 then ' ' else c end)
+      d -= 1 if c == '>'
+    end
+  end
+end
+
+ARGV.each do |f|
+  t = benchmark { strip_tags(f) }
+  STDERR.puts "#{f}: #{t}"
+end

File tests/benchmarks/ruby/utils.rb

 
   total / runs 
 end
-
-def with_utf8_file(filename)
-  File.open(filename, 'r:utf-8') do |file|
-    yield file.read
-  end
-end

File tests/benchmarks/ruby/word_count.rb

-#!/usr/bin/env ruby
-
-require './utils.rb'
-
-def word_count(str)
-  freqs = Hash.new 0
-  str.split.each do |w|
-    freqs[w.downcase] += 1
-  end
-  freqs
-end
-
-ARGV.each do |f|
-  t = benchmark { with_utf8_file(f) { |c| word_count(c) } }
-  STDERR.puts "#{f}: #{t}"
-end