Ruby 2.4.0-preview1 almost 20% faster than 2.3.1

Running the following Ruby script on my 2010 Mac (OS X 10.10) with a 19Mb log file takes 2.7 seconds with Ruby 2.3.1 but only 2.2 seconds with Ruby 2.4.0-preview1 - a speedup of almost 20%. Go get 'em Matz :slight_smile:

#!/usr/bin/env ruby

fh = File.open('logs.txt')
while fh.gets do
   print if $_ =~ /\b\w{15}\b/
end

fh.close

Python 3.5 takes 2.3 seconds so, hey, Ruby is now officially faster than Python :slight_smile:

Other contenders:

Perl 5.22 - 1.4 seconds
Node.js 6 - 0.9 seconds
PHP7 - 0.5 seconds

gvim

Hi,
could you tell us about the memory consumption for each language?
Thx Berg

This version took the same time but less memory (11Mb):

#!/usr/bin/env ruby

IO.foreach('logs.txt') do |line|
   puts line if line =~ /\b\w{15}\b/
end

By comparison:

Python 3.5 - 4.8Mb
Node.js 6.2 - between 18Mb / 22.5Mb
Perl 5.22 - 0.8Mb
PHP 7 - 4.8Mb

Code for the others:

···

On 06/07/2016 07:36, A Berger wrote:

Hi,
could you tell us about the memory consumption for each language?
Thx Berg

************************************************
#!/usr/bin/env node

var fs = require('fs'), byline = require('byline');
var stream = byline(fs.createReadStream('logs.txt', {encoding: 'utf8'}));
stream.on('data', function(line) { if (line.match(/\b\w{15}\b/)) console.log(line); });

************************************************
#!/usr/bin/env php

<?php

$fh = fopen('logs.txt', 'r');
while (($line = fgets($fh)) !== false) {
   if (preg_match('/\b\w{15}\b/', $line)) echo $line;
}

fclose($fh);

*************************************************
#!/usr/bin/env py3

import re

with open('logs.txt', 'r') as fh:
     for line in fh:
         if re.search(r'\b\w{15}\b', line): print(line, end=' ')

*************************************************
#!/usr/bin/env perl

use 5.022;

open my $fh, '<', 'logs.txt';
while (<$fh>) {
   chomp;
   say if $_ =~ /\b\w{15}\b/;
}
close $fh;