on master help sign in
More idiomatic python/ruby programs
commit4e79b4e3632a499db316c8746c8a540a8072ee85 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 15 Jun 2011, 21:25 (15 years ago) parentafb20f6
15 files changed+71−113
tests/benchmarks/python/case_map.py−7 tests/benchmarks/python/cut.py+12 tests/benchmarks/python/file_read.py−7 tests/benchmarks/python/sort.py+6−8 tests/benchmarks/python/strip_brackets.py → tests/benchmarks/python/strip_tags.py+9−6 tests/benchmarks/python/utils.py−4 tests/benchmarks/python/word_count.py−17 tests/benchmarks/ruby/case_map.rb−8 tests/benchmarks/ruby/cut.rb+16 tests/benchmarks/ruby/file_read.rb−8 tests/benchmarks/ruby/sort.rb+6−5 tests/benchmarks/ruby/strip_brackets.rb−21 tests/benchmarks/ruby/strip_tags.rb+22 tests/benchmarks/ruby/utils.rb−6 tests/benchmarks/ruby/word_count.rb−16
tests/benchmarks/python/case_map.pydeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env python
2−
3−import utils, sys
4−
5−for f in sys.argv[1:]:
6− t = utils.benchmark(lambda: utils.with_utf8_file(f, lambda c: c.upper()))
7− sys.stderr.write('{0}: {1}\n'.format(f, t))
tests/benchmarks/python/cut.pynew file+12
@@ -0,0 +1,12 @@
1+#!/usr/bin/env python
2+
3+import utils, sys, codecs
4+
5+def cut(filename, l, r):
6+ content = open(filename, encoding='utf-8')
7+ for line in content:
8+ print(line[l:r])
9+
10+for f in sys.argv[1:]:
11+ t = utils.benchmark(lambda: cut(f, 20, 40))
12+ sys.stderr.write('{0}: {1}\n'.format(f, t))
tests/benchmarks/python/file_read.pydeleted−7
@@ -1,7 +0,0 @@
1−#!/usr/bin/env python
2−
3−import utils, sys
4−
5−for f in sys.argv[1:]:
6− t = utils.benchmark(lambda: utils.with_utf8_file(f, lambda c: len(c)))
7− sys.stderr.write('{0}: {1}\n'.format(f, t))
tests/benchmarks/python/sort.py+6−8
@@ -1,15 +1,13 @@
11#!/usr/bin/env python
22
3−import utils, sys
3+import utils, sys, codecs
44
5−def sort(string):
6− lines = string.splitlines()
5+def sort(filename):
6+ content = open(filename, encoding='utf-8').read()
7+ lines = content.splitlines()
78 lines.sort()
8− return '\n'.join(lines)
9+ print('\n'.join(lines))
910
1011for f in sys.argv[1:]:
11− t = utils.benchmark(lambda: sys.stdout.write(
12− utils.with_utf8_file(f,sort).encode('utf-8'))
13− )
12+ t = utils.benchmark(lambda: sort(f))
1413 sys.stderr.write('{0}: {1}\n'.format(f, t))
15−
tests/benchmarks/python/strip_brackets.py → tests/benchmarks/python/strip_tags.pyrenamed+9−6
@@ -2,21 +2,24 @@
22
33import utils, sys
44
5−def strip_brackets(string):
5+def strip_tags(filename):
6+ string = open(filename, encoding='utf-8').read()
7+
68 d = 0
7− out = ''
9+ out = []
10+
811 for c in string:
9− if c == '{' or c == '[': d += 1
12+ if c == '<': d += 1
1013
1114 if d > 0:
1215 out += ' '
1316 else:
1417 out += c
1518
16− if c == '}' or c == ']': d -= 1
19+ if c == '>': d -= 1
1720
18− return out
21+ print(''.join(out))
1922
2023for f in sys.argv[1:]:
21− t = utils.benchmark(lambda: utils.with_utf8_file(f, strip_brackets))
24+ t = utils.benchmark(lambda: strip_tags(f))
2225 sys.stderr.write('{0}: {1}\n'.format(f, t))
tests/benchmarks/python/utils.py−4
@@ -16,7 +16,3 @@ def benchmark(f):
1616 sys.stderr.write('Run {0}: {1}\n'.format(i, result))
1717 total += result
1818 return total / runs
19−
20−def with_utf8_file(filename, f):
21− contents = open(filename).read().decode('utf-8')
22− return f(contents)
tests/benchmarks/python/word_count.pydeleted−17
@@ -1,17 +0,0 @@
1−#!/usr/bin/env python
2−
3−import utils, sys
4−
5−def word_count(string):
6− freqs = {}
7− for w in string.split():
8− w = w.lower()
9− if freqs.get(w):
10− freqs[w] += 1
11− else:
12− freqs[w] = 1
13− return freqs
14−
15−for f in sys.argv[1:]:
16− t = utils.benchmark(lambda: utils.with_utf8_file(f, word_count))
17− sys.stderr.write('{0}: {1}\n'.format(f, t))
tests/benchmarks/ruby/case_map.rbdeleted−8
@@ -1,8 +0,0 @@
1−#!/usr/bin/env ruby
2−
3−require './utils.rb'
4−
5−ARGV.each do |f|
6− t = benchmark { with_utf8_file(f) { |c| c.upcase } }
7− STDERR.puts "#{f}: #{t}"
8−end
tests/benchmarks/ruby/cut.rbnew file+16
@@ -0,0 +1,16 @@
1+#!/usr/bin/env ruby
2+
3+require './utils.rb'
4+
5+def cut(filename, l, r)
6+ File.open(filename, 'r:utf-8') do |file|
7+ file.each_line do |line|
8+ puts line[l, r - l]
9+ end
10+ end
11+end
12+
13+ARGV.each do |f|
14+ t = benchmark { cut(f, 20, 40) }
15+ STDERR.puts "#{f}: #{t}"
16+end
tests/benchmarks/ruby/file_read.rbdeleted−8
@@ -1,8 +0,0 @@
1−#!/usr/bin/env ruby
2−
3−require './utils.rb'
4−
5−ARGV.each do |f|
6− t = benchmark { with_utf8_file(f) { |c| c.size } }
7− STDERR.puts "#{f}: #{t}"
8−end
tests/benchmarks/ruby/sort.rb+6−5
@@ -2,13 +2,14 @@
22
33require './utils.rb'
44
5−def sort(str)
6− str.lines.sort.join
5+def sort(filename)
6+ File.open(filename, 'r:utf-8') do |file|
7+ content = file.read
8+ puts content.lines.sort.join
9+ end
710end
811
912ARGV.each do |f|
10− t = benchmark do
11− with_utf8_file(f) { |c| puts sort(c) }
12− end
13+ t = benchmark { sort(f) }
1314 STDERR.puts "#{f}: #{t}"
1415end
tests/benchmarks/ruby/strip_brackets.rbdeleted−21
@@ -1,21 +0,0 @@
1−#!/usr/bin/env ruby
2−
3−require './utils.rb'
4−
5−def strip_brackets(str)
6− d = 0
7− out = ''
8−
9− str.each_char do |c|
10− d += 1 if c == '{' || c == '['
11− out << if d > 0 then ' ' else c end
12− d -= 1 if c == '}' || c == ']'
13− end
14−
15− out
16−end
17−
18−ARGV.each do |f|
19− t = benchmark { with_utf8_file(f) { |c| strip_brackets(c) } }
20− STDERR.puts "#{f}: #{t}"
21−end
tests/benchmarks/ruby/strip_tags.rbnew file+22
@@ -0,0 +1,22 @@
1+#!/usr/bin/env ruby
2+
3+require './utils.rb'
4+
5+def strip_tags(filename)
6+ File.open(filename, 'r:utf-8') do |file|
7+ str = file.read
8+
9+ d = 0
10+
11+ str.each_char do |c|
12+ d += 1 if c == '<'
13+ putc(if d > 0 then ' ' else c end)
14+ d -= 1 if c == '>'
15+ end
16+ end
17+end
18+
19+ARGV.each do |f|
20+ t = benchmark { strip_tags(f) }
21+ STDERR.puts "#{f}: #{t}"
22+end
tests/benchmarks/ruby/utils.rb−6
@@ -12,9 +12,3 @@ def benchmark(&block)
1212
1313 total / runs
1414end
15−
16−def with_utf8_file(filename)
17− File.open(filename, 'r:utf-8') do |file|
18− yield file.read
19− end
20−end
tests/benchmarks/ruby/word_count.rbdeleted−16
@@ -1,16 +0,0 @@
1−#!/usr/bin/env ruby
2−
3−require './utils.rb'
4−
5−def word_count(str)
6− freqs = Hash.new 0
7− str.split.each do |w|
8− freqs[w.downcase] += 1
9− end
10− freqs
11−end
12−
13−ARGV.each do |f|
14− t = benchmark { with_utf8_file(f) { |c| word_count(c) } }
15− STDERR.puts "#{f}: #{t}"
16−end