Rio: which is the slow one?

O

Oliver Cromm

The speed difference looks too extreme too me:


caps = []
File.open('caps_u8.dic').each {|line| caps << line.split(';')[0]}

=> 1.8 seconds



require 'rio'
caps = rio('caps_u8.dic').csv(";").columns(0)[].flatten
p caps

=> 50.9 seconds


What exactly is so slow here? It's not the /flatten/.
Am I using Rio in a particularly dumb way?
 
M

Meinrad Recheis

------=_Part_19197_19575641.1141195367544
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

you could use the ruby profiler to find out exactly ....
-- henon

The speed difference looks too extreme too me:


caps =3D []
File.open('caps_u8.dic').each {|line| caps << line.split(';')[0]}

=3D> 1.8 seconds



require 'rio'
caps =3D rio('caps_u8.dic').csv(";").columns(0)[].flatten
p caps

=3D> 50.9 seconds


What exactly is so slow here? It's not the /flatten/.
Am I using Rio in a particularly dumb way?

------=_Part_19197_19575641.1141195367544--
 
J

James Edward Gray II

The speed difference looks too extreme too me:


caps = []
File.open('caps_u8.dic').each {|line| caps << line.split(';')[0]}

=> 1.8 seconds

Here you are rolling your own split.
require 'rio'
caps = rio('caps_u8.dic').csv(";").columns(0)[].flatten
p caps

=> 50.9 seconds

And here Rio is using CSV, which is known to be plenty slow. If we
could get Rio patched to use FasterCSV when it is available, that
would help quite a bit...

James Edward Gray II
 
R

rio4ruby

James said:
The speed difference looks too extreme too me:


caps = []
File.open('caps_u8.dic').each {|line| caps << line.split(';')[0]}

=> 1.8 seconds

Here you are rolling your own split.
require 'rio'
caps = rio('caps_u8.dic').csv(";").columns(0)[].flatten
p caps

=> 50.9 seconds

This is a false comparison. The speedy code will not properly parse
many CSV files.

For example, the following is a legal line from a CSV file:

"Field 1","Hello, World", "Field 3"

The comma embedded in the second field precludes the use of a simple
+split+. In addition, the speedy version would include the double
quotes in the field value -- which is incorrect.

-Christopher
 
O

Oliver Cromm

rio4ruby said:
James said:
The speed difference looks too extreme too me:


caps = []
File.open('caps_u8.dic').each {|line| caps << line.split(';')[0]}

=> 1.8 seconds

Here you are rolling your own split.
require 'rio'
caps = rio('caps_u8.dic').csv(";").columns(0)[].flatten
p caps

=> 50.9 seconds

This is a false comparison. The speedy code will not properly parse
many CSV files.

I didn't claim they are equivalent in principle; but for the purpose at
hand, they are. And in this case, I wouldn't have cared if one version
takes 5 times as long, but 25 times is not practicable - that speed
difference would easily justify, say, 15 minutes more time for
programming, so I could cover a lot of cases.
For example, the following is a legal line from a CSV file:

"Field 1","Hello, World", "Field 3"

I doubt that a split(/\",\s*\"/) (plus necessary adjustments) would be
much slower.
 
R

rio4ruby

Oliver said:
The speed difference looks too extreme too me:
...
What exactly is so slow here?

Good Question. The first problem is that you are using a general
purpose CSV parser to split strings. However, the difference you
report is too extreme for that to be the only issue.

I created four test cases:

ruby split:
caps = []
File.open(fn).each {|line| caps << line.chomp.split(',')[0] }

rio split:
caps = []
rio(fn).chomp.lines { |line| caps << line.split(',')[0] }

stdlib csv:
caps = []
File.open(fn).each {|line| caps << CSV.parse_line(line)[0] }

rio csv:
caps = rio(fn).csv.columns(0)[].flatten

Benchmarking these cases on a 10000 line CSV file yielded:
ruby split: 0.516000
rio split : 0.984000
stdlib csv: 3.047000
rio csv : 15.610000

This shows that Rio incurs a 2x overhead when reading lines from a
file, which is reasonable, considering the features of Rio not
illustrated in this trivial example.

Using the standard library's CSV incurs 6x overhead, which seems a bit
high but is not unreasonable, considering the difference in complexity
between splitting a string and parsing a CSV line. The CSV module
could probably be more efficient.

Using Rio to call the standard library's CSV incurs a 5x overhead
above calling the standard library's CSV. This yields an overhead of
30x compared to the stdlib split. This is close to what you report
(28x).

The 5x overhead incured when using Rio to call CSV does seem too
high. One would expect it to be closer to 2x.

The reason for the high overhead is the feature of Rio that extends
every Array returned from a CSV file with a custom +to_s+ method,
which will convert the Array back to a CSV line. Without this feature
the "rio csv" case yields:

rio csv : 5.750000

which is a 1.9x over the stdlib CSV.

I was dubious that extending each Array was a good thing even if it
cost nothing. It is certainly not a good thing with such a high
perfomance penalty. I will remove this feature in the next release.
Beyond this, the only thing that will make Rio's handling of CSV files
is a faster CSV module (FasterCSV perhaps) and perfomance improvements
in Rio, which will be addressed when Rio reaches Alpha.

Thanks for bringing this to my attention.

Cheers,
-Christopher
 
J

James Edward Gray II

Using the standard library's CSV incurs 6x overhead, which seems a bit
high but is not unreasonable, considering the difference in complexity
between splitting a string and parsing a CSV line. The CSV module
could probably be more efficient.

See FasterCSV. ;)

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top