Browse files

Prefer File.read over File.open.read

  • Loading branch information...
1 parent 517cfbe commit 9d2187efb7da3c531997eb0261bc39cf48a69821 @amatsuda amatsuda committed Feb 1, 2013
Showing with 1 addition and 1 deletion.
  1. +1 −1 railties/lib/rails/commands/application.rb
View
2 railties/lib/rails/commands/application.rb
@@ -12,7 +12,7 @@
unless ARGV.delete("--no-rc")
railsrc = File.join(File.expand_path("~"), ".railsrc")
if File.exist?(railsrc)
- extra_args_string = File.open(railsrc).read
+ extra_args_string = File.read(railsrc)
extra_args = extra_args_string.split(/\n+/).map {|l| l.split}.flatten
puts "Using #{extra_args.join(" ")} from #{railsrc}"
ARGV.insert(1, *extra_args)

4 comments on commit 9d2187e

@smondal

Hi amatsuda,

I have check, File open and then read is faster.
Please check the benchmark
Benchmark.bm do |x|
1.9.3-p362 :006 > x.report { 1000.times do ; File.open("nginx.conf").read; end }
1.9.3-p362 :007?> end
user system total real
0.020000 0.010000 0.030000 ( 0.029208)
=> [ 0.020000 0.010000 0.030000 ( 0.029208)
]
1.9.3-p362 :008 > Benchmark.bm do |x|
1.9.3-p362 :009 > x.report { 10000.times do ; File.read("nginx.conf"); end }
1.9.3-p362 :010?> end
user system total real
0.150000 0.040000 0.190000 ( 0.197419)
=> [ 0.150000 0.040000 0.190000 ( 0.197419)

And I have checked it two three times.

@carlosantoniodasilva
Ruby on Rails member

@smondal thanks for the benchmark. In any case, this code is only executed when creating a new application and you have a ~/.railsrc file to add common options to new rails applications, so it shouldn't be a big deal.

@amatsuda
Ruby on Rails member

@smondal Looks like you benchmarked File.open + read 1000.times 🆚 File.read 10000.times.

Please sign in to comment.