Skip to content

Commit

Permalink
Prefer File.read over File.open.read
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Feb 1, 2013
1 parent 517cfbe commit 9d2187e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/application.rb
Expand Up @@ -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)
Expand Down

4 comments on commit 9d2187e

@smondal
Copy link

@smondal smondal commented on 9d2187e Feb 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@smondal
Copy link

@smondal smondal commented on 9d2187e Feb 3, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.