Jump to content

Rate counter: Difference between revisions

no edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
No edit summary
Line 396:
Call the <code>time-this</code> macro to excute a loop 99 times:
<lang lisp>(print (time-this 99 (loop for i below 10000 sum i)))</lang>which gives a pair of numbers, the real time and the run time, both in seconds:<lang>(0.023 0.022)</lang>
 
=={{header|Crystal}}==
{{trans|Ruby}}
Testing lookup speed in array versus hash:
<lang ruby>require "benchmark"
 
struct Document
property :id
def initialize(@id : Int32) end
end
 
documents_a = [] of Int32 | Document
documents_h = {} of Int32 => Int32 | Document
 
1.upto(10_000) do |n|
d = Document.new(n)
documents_a << d
documents_h[d.id] = d
end
 
searchlist = Array.new(1000){ rand(10_000)+1 }
Benchmark.bm do |x|
x.report("array"){searchlist.each{ |el| documents_a.any?{ |d| d == el }} }
x.report("hash") {searchlist.each{ |el| documents_h.has_key?(el) } }
end
puts
Benchmark.ips do |x|
x.report("array"){searchlist.each{ |el| documents_a.any?{ |d| d == el }} }
x.report("hash") {searchlist.each{ |el| documents_h.has_key?(el) } }
end</lang>
{{out}}
<pre> user system total real
array 0.011229 0.000130 0.011359 ( 0.011358)
hash 0.000112 0.000016 0.000128 ( 0.000128)
 
array 95.46 ( 10.48ms) (± 0.64%) 0.0B/op 666.36× slower
hash 63.61k ( 15.72µs) (± 1.23%) 0.0B/op fastest
</pre>
 
=={{header|D}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.