Skip to content

Instantly share code, notes, and snippets.

@knowtheory
Last active December 11, 2015 05:08
Show Gist options
  • Select an option

  • Save knowtheory/4549975 to your computer and use it in GitHub Desktop.

Select an option

Save knowtheory/4549975 to your computer and use it in GitHub Desktop.
n = 50000;
Benchmark.bm(15) do |x|
x.report("Array Include:"){ n.times{ foo = 5; arr = [1,2,3,4,5]; arr.include? foo }}
x.report("Array access:"){ n.times{ foo = 5; arr = [1,2,3,4,5]; foo == arr[0] || foo == arr[1] || foo == arr[2] || foo == arr[3] || foo == arr[4] } }
x.report("Array Alloc:"){ n.times{ foo = 5; arr = [1,2,3,4,5]; foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 } }
x.report("Just Ors:"){ n.times{foo = 5; foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 }}
end
n = 50000;
foo = 1
Benchmark.bm(15) do |x|
x.report("Array Include:"){ n.times{ arr = [1,2,3,4,5]; arr.include? foo }}
x.report("Array access:"){ n.times{ arr = [1,2,3,4,5]; foo == arr[0] || foo == arr[1] || foo == arr[2] || foo == arr[3] || foo == arr[4] } }
x.report("Array Alloc:"){ n.times{ arr = [1,2,3,4,5]; foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 } }
x.report("Just Ors:"){ n.times{ foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 }}
end
n = 50000;
foo = 5
Benchmark.bm(15) do |x|
x.report("Array Include:"){ n.times{ arr = [1,2,3,4,5]; arr.include? foo }}
x.report("Array access:"){ n.times{ arr = [1,2,3,4,5]; foo == arr[0] || foo == arr[1] || foo == arr[2] || foo == arr[3] || foo == arr[4] } }
x.report("Array Alloc:"){ n.times{ arr = [1,2,3,4,5]; foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 } }
x.report("Just Ors:"){ n.times{ foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 }}
end
n = 50000;
foo = 5
arr = [1,2,3,4,5]
Benchmark.bm(15) do |x|
x.report("Array Include:"){ n.times{ arr.include? foo }}
x.report("Array access:"){ n.times{ foo == arr[0] || foo == arr[1] || foo == arr[2] || foo == arr[3] || foo == arr[4] } }
x.report("Just Ors:"){ n.times{ foo == 1 || foo == 2 || foo == 3 || foo == 4 || foo == 5 }}
end
user system total real
Array Include: 0.030000 0.000000 0.030000 ( 0.026587)
Array access: 0.030000 0.000000 0.030000 ( 0.025596)
Array Alloc: 0.020000 0.000000 0.020000 ( 0.023778)
Just Ors: 0.010000 0.000000 0.010000 ( 0.008670)
user system total real
Array Include: 0.020000 0.000000 0.020000 ( 0.020130)
Array access: 0.020000 0.000000 0.020000 ( 0.017447)
Array Alloc: 0.020000 0.000000 0.020000 ( 0.017289)
Just Ors: 0.000000 0.000000 0.000000 ( 0.003805)
user system total real
Array Include: 0.030000 0.000000 0.030000 ( 0.028463)
Array access: 0.020000 0.000000 0.020000 ( 0.026253)
Array Alloc: 0.020000 0.000000 0.020000 ( 0.021645)
Just Ors: 0.010000 0.000000 0.010000 ( 0.008326)
user system total real
Array Include: 0.020000 0.000000 0.020000 ( 0.013915)
Array access: 0.010000 0.000000 0.010000 ( 0.011520)
Just Ors: 0.010000 0.000000 0.010000 ( 0.008622)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment