Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created July 7, 2011 18:48
Show Gist options
  • Save vysogot/1070237 to your computer and use it in GitHub Desktop.
Save vysogot/1070237 to your computer and use it in GitHub Desktop.
Cartesian product
def cartprod(*args)
result = [[]]
while [] != args
t, result = result, []
b, *args = args
t.each do |a|
b.each do |n|
result << a + [n]
end
end
end
result
end
# Posted here http://www.ruby-forum.com/topic/95519
@vysogot
Copy link
Author

vysogot commented Jul 7, 2011

10 element 100 arrays 100 times 0.010000 0.000000 0.010000 ( 0.004783)
10 element 100 arrays 1000 times 0.030000 0.000000 0.030000 ( 0.037841)
10 element 100 arrays 10000 times 0.360000 0.000000 0.360000 ( 0.357748)

100 element 100 arrays 100 times 0.000000 0.000000 0.000000 ( 0.003819)
100 element 100 arrays 1000 times 0.030000 0.000000 0.030000 ( 0.035036)
100 element 100 arrays 10000 times 0.380000 0.000000 0.380000 ( 0.379334)

10 element 1000 arrays 100 times 0.050000 0.000000 0.050000 ( 0.054502)
10 element 1000 arrays 1000 times 0.430000 0.000000 0.430000 ( 0.441362)
10 element 1000 arrays 10000 times 3.530000 0.010000 3.540000 ( 3.575773)

100 element 1000 arrays 100 times 0.050000 0.000000 0.050000 ( 0.042936)
100 element 1000 arrays 1000 times 0.430000 0.010000 0.440000 ( 0.432864)
100 element 1000 arrays 10000 times 4.330000 0.010000 4.340000 ( 4.361241)

10 element 10000 arrays 100 times 0.470000 0.010000 0.480000 ( 0.506429)
10 element 10000 arrays 1000 times 4.700000 0.020000 4.720000 ( 4.765935)
10 element 10000 arrays 10000 times 47.370000 0.220000 47.590000 ( 48.169544)

100 element 10000 arrays 100 times 0.470000 0.010000 0.480000 ( 0.471123)
100 element 10000 arrays 1000 times 5.950000 0.020000 5.970000 ( 6.011974)
100 element 10000 arrays 10000 times 60.820000 0.390000 61.210000 ( 63.746835)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment