Hello, I was reading the pickaxe book and then I arrived to the chapter "When trouble strikes" in the Benchmark section. I have this code: #!/usr/bin/ruby -w require ''benchmark'' include Benchmark LOOP_COUNT = 1_000_000 bm(12) do |test| test.report("normal:") do LOOP_COUNT.times do |x| y = x +1 end end test.report("predefine:") do x = y = 0 LOOP_COUNT.times do |x| y = x + 1 end end end The "predefine" test is more fast than the "normal". Why? The only difference that I see is x = y = 0 but I don''t understand thanks! Rafael G.
Rafa G. wrote:> Hello, > > I was reading the pickaxe book and then I arrived to the chapter > "When trouble strikes" in the Benchmark section. > > I have this code: > > #!/usr/bin/ruby -w > require ''benchmark'' > include Benchmark > > LOOP_COUNT = 1_000_000 > > bm(12) do |test| > test.report("normal:") do > LOOP_COUNT.times do |x| > y = x +1 > end > end > test.report("predefine:") do > x = y = 0 > LOOP_COUNT.times do |x| > y = x + 1 > end > end > end > > The "predefine" test is more fast than the "normal". Why? > The only difference that I see is x = y = 0 but I don''t understand > > thanks!Because of scope. In the second test, x and y are defined in the scope outside the code block, and the block uses the outside variables. In the first block, the variables are created for each iteration of the loop.> > > Rafael G. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks! Inge J?rgensen escribi?:> Rafa G. wrote: >> Hello, >> >> I was reading the pickaxe book and then I arrived to the chapter >> "When trouble strikes" in the Benchmark section. >> >> I have this code: >> >> #!/usr/bin/ruby -w >> require ''benchmark'' >> include Benchmark >> >> LOOP_COUNT = 1_000_000 >> >> bm(12) do |test| >> test.report("normal:") do >> LOOP_COUNT.times do |x| >> y = x +1 >> end >> end >> test.report("predefine:") do >> x = y = 0 >> LOOP_COUNT.times do |x| >> y = x + 1 >> end >> end >> end >> >> The "predefine" test is more fast than the "normal". Why? >> The only difference that I see is x = y = 0 but I don''t understand >> >> thanks! > Because of scope. In the second test, x and y are defined in the scope > outside the code block, and the block uses the outside variables. In the > first block, the variables are created for each iteration of the loop. > >> >> >> Rafael G. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Maybe Matching Threads
- (A bit offtopic): A good starting book for ruby programming?
- 3 commits - libswfdec/swfdec_audio_event.c libswfdec/swfdec_sound.c
- Pref for beginner book: Wrox (Holzner) or Ruby for Rails?
- Rails 1.0 - Agile book still good?
- 12 commits - configure.ac doc/Makefile.am libswfdec/swfdec_as_frame.c libswfdec/swfdec_audio.c libswfdec/swfdec_audio_event.c libswfdec/swfdec_audio_event.h libswfdec/swfdec_shape_parser.c libswfdec/swfdec_sound.c test/sound