Commit 93561e24 authored by Bodigrim's avatar Bodigrim Committed by Xia Li-yao
Browse files

Remove Python and Ruby code

parent 1eb22838
#!/usr/bin/env python3
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))
#!/usr/bin/env python3
import math
import sys
import time
def find_first():
cf = contents.find
return timer(lambda: cf("en:Benin"))
def timer(f, count=100):
a = 1e300
def g():
return
for i in range(3):
start = time.time()
for j in range(count):
g()
a = min(a, (time.time() - start) / count)
b = 1e300
for i in range(3):
start = time.time()
for j in range(count):
f()
b = min(b, (time.time() - start) / count)
return round(b - a, int(round(math.log(count, 10) - math.log(b - a, 10))))
contents = open('../../tests/text-test-data/yiwiki.xml', 'r').read()
benchmarks = (
find_first,
)
to_run = sys.argv[1:]
bms = []
if to_run:
for r in to_run:
for b in benchmarks:
if b.__name__.startswith(r):
bms.append(b)
else:
bms = benchmarks
for b in bms:
sys.stdout.write(b.__name__ + ' ')
sys.stdout.flush()
print(b())
#!/usr/bin/env python3
import utils, sys, codecs
def sort(filename):
content = open(filename, encoding='utf-8').read()
lines = content.splitlines()
lines.sort()
print('\n'.join(lines))
for f in sys.argv[1:]:
t = utils.benchmark(lambda: sort(f))
sys.stderr.write('{0}: {1}\n'.format(f, t))
#!/usr/bin/env python3
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))
#!/usr/bin/env python3
import sys, time
def benchmark_once(f):
start = time.time()
f()
end = time.time()
return end - start
def benchmark(f):
runs = 100
total = 0.0
for i in range(runs):
result = benchmark_once(f)
sys.stderr.write('Run {0}: {1}\n'.format(i, result))
total += result
return total / runs
#!/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
#!/usr/bin/env ruby
require './utils.rb'
def fold(filename, max_width)
File.open(filename, 'r:utf-8') do |file|
# Words in this paragraph
paragraph = []
file.each_line do |line|
# If we encounter an empty line, we reformat and dump the current
# paragraph
if line.strip.empty?
puts fold_paragraph(paragraph, max_width)
puts
paragraph = []
# Otherwise, we append the words found in the line to the paragraph
else
paragraph.concat line.split
end
end
# Last paragraph
puts fold_paragraph(paragraph, max_width) unless paragraph.empty?
end
end
# Fold a single paragraph to the desired width
def fold_paragraph(paragraph, max_width)
# Gradually build our output
str, *rest = paragraph
width = str.length
rest.each do |word|
if width + word.length + 1 <= max_width
str << ' ' << word
width += word.length + 1
else
str << "\n" << word
width = word.length
end
end
str
end
ARGV.each do |f|
t = benchmark { fold(f, 80) }
STDERR.puts "#{f}: #{t}"
end
#!/usr/bin/env ruby
require './utils.rb'
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 { sort(f) }
STDERR.puts "#{f}: #{t}"
end
#!/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
require 'benchmark'
def benchmark(&block)
runs = 100
total = 0
runs.times do |i|
result = Benchmark.measure(&block).total
$stderr.puts "Run #{i}: #{result}"
total += result
end
total / runs
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment