I have done #1 of Project Euler's in Ruby. Using two different approaches. One uses set, another uses reduce. require 'set'start = Time.nows1 = Set.new (1..1000/3.floor).map { |x| 3 * x }s2 = Set.new (1..1000/5.floor).map { |x| 5 * x }sum = s1.merge(s2).to_a.inject(:+)duration = Time.now - start puts Sum: #{sum}, time elapsed #{duration} sSecond methodstart = Time.nowsum = (1..1000).select{ |x| x % 3 == 0 || x % 5 == 0 }.reduce(:+)duration = Time.now - startThe second method consistently performs better than first method:method 1: time elapsed 0.000364 smethod 2: time elapsed 0.000198 sI wonder if it is a correct way to time how long the process takes. And whether it is considered to be performant. Also curious how other languages performs compares to this two algo. Any advice are welcomed~
Project Euler #1 in Ruby with duration
performance;ruby;programming challenge
Performing benchmarks with such small values is pretty much meaningless, you will usually be concerned with larger values. And this is how you  tipically compare the time performance of different snippets:require 'benchmark'require 'set'n = 1_000_000Benchmark.bm do |b|  b.report(sets) do    s1 = Set.new (1..n/3.floor).map { |x| 3 * x }    s2 = Set.new (1..n/5.floor).map { |x| 5 * x }    sum = s1.merge(s2).inject(0, :+)  end  b.report(select) do    sum = (1..n).select { |x| x % 3 == 0 || x % 5 == 0 }.reduce(0, :+)  endendOutput:         user     system      total        realsets    0.550000   0.020000   0.570000 (  0.580492)select  0.200000   0.000000   0.200000 (  0.207238)