3 ms·
On my 2013 Macbook Pro, using Ruby 2.6.5 and Python 3.7.4, I wrote two parsers for a 19Mb Apache log file - logs1.txt. Code for each language: Ruby IO.forea
by cutler 7y ago
On my 2013 Macbook Pro, using Ruby 2.6.5 and Python 3.7.4, I wrote two parsers for a 19Mb Apache log file - logs1.txt. Code for each language:
Ruby
IO.foreach('logs1.txt') {|x| puts x if /\b\w{15}\b/.match? x }
Python
with open('logs1.txt', 'r') as fh:
for line in fh:
if re.search(r'\b\w{15}\b', line): print(line, end='')
Ruby's best was 3.3 seconds whilst Python's was 3.9 seconds. If you factor-out Ruby taking 100ms longer than Python to startup Ruby is 18% faster than Python for string parsing. Results probably differ for Python's numeric performance but that depends whether you're including C-based third party libs such as numpy.