Regis d’Aubarede wrote in post #1165145:
Robert K. wrote in post #1165134:
Regis d’Aubarede wrote in post #1165078:
Robert K. wrote in post #1165068:
In what ways is that better than my approach? At the moment it just
seems more complicated but maybe I am overlooking something.
get_var10 is only 3 times slower than get_var !
I fail to see how your example answers even the question of mine that
you quoted
My exemple show that the measure with a simple n.times { code }
is not lineare with code repetition :
if code is repeated n times, measure is not multiply by n.
Maybe I did not get what you meant with “linear” here. I think now you
mean “linear in the number of repetitions”: With
require ‘benchmark’
REP = 10.times.map {|i| 10**i}
Benchmark.bmbm do |x|
REP.each do |rep|
x.report “%10d” % rep do
rep.times {}
end
end
end
I get
$ ./x.rb
Rehearsal ----------------------------------------------
1 0.000000 0.000000 0.000000 ( 0.000007)
10 0.000000 0.000000 0.000000 ( 0.000005)
100 0.000000 0.000000 0.000000 ( 0.000012)
1000 0.000000 0.000000 0.000000 ( 0.000095)
10000 0.010000 0.000000 0.010000 ( 0.000916)
100000 0.000000 0.000000 0.000000 ( 0.011101)
1000000 0.060000 0.000000 0.060000 ( 0.060148)
10000000 0.600000 0.000000 0.600000 ( 0.596774)
100000000 5.970000 0.000000 5.970000 ( 5.967721)
1000000000 59.540000 0.010000 59.550000 ( 59.558273)
------------------------------------ total: 66.190000sec
user system total real
1 0.000000 0.000000 0.000000 ( 0.000003)
10 0.000000 0.000000 0.000000 ( 0.000003)
100 0.000000 0.000000 0.000000 ( 0.000008)
1000 0.000000 0.000000 0.000000 ( 0.000059)
10000 0.000000 0.000000 0.000000 ( 0.000569)
100000 0.010000 0.000000 0.010000 ( 0.005671)
1000000 0.060000 0.000000 0.060000 ( 0.057733)
10000000 0.570000 0.000000 0.570000 ( 0.570455)
100000000 5.710000 0.000000 5.710000 ( 5.716101)
1000000000 57.120000 0.000000 57.120000 ( 57.122374)
That looks pretty linear to me.
The point is that you seem to employ a more complicated scheme to
measure performance than the simple one I used and I would like to know
what the advantage is. After all
with my methods, the measure is linear with repetition, I conclude that
it is better…
see above
and now how many member variable and plus operations shadow
the cost of a method call.
I add the plus because it’s seem that ruby 2.1 has some new optimization
:
@v;@v;@v;@v;@v;@v;@v;@v;@v;@v
take almost same time than @v
For me that would be one more reason to not do this.
let alone the other ones (questions)…
And how do you stop the infinite loop?
How do you make sure you do not measure the repetition loop?
My code is :
def tst() code;code;… end # 100 times same code
def tst0(call_time)
t=Time.now.to_f
target=t+10
i=0
while Time.now.to_f<target
tst();tst(); … # 400 times
i+=1
end
((Time.now.to_f - t)1000_000_000 - call_timei1000)/(i400*100))
end
Why do you convert the time to a float? At least for the loop (variable
“target”) that is superfluous because the precision is in the Time
instance already:
irb(main):005:0> 10.times.map {Time.now}.each_cons(2){|a,b| p b-a}
1.955e-06
1.048e-06
9.78e-07
1.327e-06
9.77e-07
9.78e-07
9.78e-07
9.08e-07
9.78e-07
=> nil
irb(main):006:0> 10.times.map {Time.now}.each_cons(2){|a,b| p a<b}
true
true
true
true
true
true
true
true
true
Time.now.to_s take ~ 500 ns and call tst() ( call_time ) has be measured
with an empty code.
I achieved the same with REP { } (i.e. empty block): with that I
calculated the overhead of the looping and subtracted that from the
other measurements.
This solution is complex because I try to get a measure exact.It is
difficult when the code to test is very simple.
So with that complexity you are introducing an error of its own. 
Then we should use an ARM configuration and a oscilloscope for check
the exact timing 
Any measurement in a GCed language has some level of uncertainty because
one does not control when GC kicks in. Even your oscilloscope won’t
help with that, I guess.
Hoping to have answered all your questions…
Regards,
Most of them, I think. I still think the complexity introduced is not
outweighed by the advantages. You have to create a baseline with empty
code as well - the same way as I do.
Btw. you can fairly easy automate that because you can calculate with
Benchmark::Tms:
irb(main):026:0> t1=nil;Benchmark.bm {|x| t1 = x.report(“a”){sleep 1}}
user system total real
a 0.000000 0.000000 0.000000 ( 1.000098)
=> [ 0.000000 0.000000 0.000000 ( 1.000098)
]
irb(main):027:0> t2=nil;Benchmark.bm {|x| t2 = x.report(“a”){sleep 1.2}}
user system total real
a 0.000000 0.000000 0.000000 ( 1.200099)
=> [ 0.000000 0.000000 0.000000 ( 1.200099)
]
irb(main):028:0> t2 - t1
=> 0.000000 0.000000 0.000000 ( 0.200001)
irb(main):029:0> (t2 - t1).class
=> Benchmark::Tms
This gives me an idea for a feature request. 
(sorry for my English, I prefer Ruby language…)
