Marshal.load Speed

Eric Hodel | Wed, 03 May 2006 04:52:41 GMT

Posted in ,

File.open 'dump' do |fp| Marshal.load fp end

Is much slower than:

File.open 'dump' do |fp| Marshal.load fp.read end

Because the former uses IO#getc and the latter operates directly on a String.

I learned this tonight while helping profile code at a Seattle.rb hacking night.

2 comments

Comments RSS FEED

With the “I’m sure you know it but I’ll say it for everyone else’s benefit” trade-off that the latter is much less memory efficient since the whole file must be read in to memory before processing can begin. Of course I’m usually loading small files, so for me at least that’s not a big downside – just something to consider.

Thanks for the tip!

Nathaniel Talbott said about 10 hours later

That’s true Nathaniel, but I would think there would be a happier medium using line based reading instead of character based (ick). I haven’t looked at the innards of Marshal, though, so maybe there is no other way.

Daniel Berger said 1 day later

Comments are disabled