Last active
August 29, 2015 14:24
-
-
Save gilomen2/471455e48a8b90eeee32 to your computer and use it in GitHub Desktop.
Benchmark binary search vs. iterative search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
Binary - Bill: 0.000000 0.000000 0.000000 ( 0.000014) | |
Iterative - Bill: 0.000000 0.000000 0.000000 ( 0.000012) | |
Binary - Bob: 0.000000 0.000000 0.000000 ( 0.000020) | |
Iterative - Bob: 0.000000 0.000000 0.000000 ( 0.000007) | |
Binary - Joe: 0.000000 0.000000 0.000000 ( 0.000005) | |
Iterative - Joe: 0.000000 0.000000 0.000000 ( 0.000005) | |
Binary - Sally: 0.000000 0.000000 0.000000 ( 0.000007) | |
Iterative - Sally: 0.000000 0.000000 0.000000 ( 0.000005) | |
Binary - Sussie: 0.000000 0.000000 0.000000 ( 0.000007) | |
Iterative - Sussie: 0.000000 0.000000 0.000000 ( 0.000005) | |
Binary - None: 0.000000 0.000000 0.000000 ( 0.000006) | |
Iterative - None: 0.000000 0.000000 0.000000 ( 0.000005) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def binary_search(name, array) | |
lower = 0 | |
upper = array.length - 1 | |
while lower <= upper | |
mid = (lower + upper) / 2 | |
mid_name = array[mid] | |
if name == mid_name | |
return array[mid] | |
elsif name < mid_name | |
upper = mid - 1 | |
elsif name> mid_name | |
lower = mid + 1 | |
end | |
end | |
return nil | |
end | |
def iterative_search(name, array) | |
i = 0 | |
while i <= (array.length - 1) | |
if array[i] == name | |
return array[i] | |
else | |
i += 1 | |
end | |
end | |
return nil | |
end | |
array_to_search = %w[Bill Bob Joe Sally Sussie] | |
Benchmark.bm do |x| | |
x.report("Binary - Bill: ") {binary_search("Bill", array_to_search)} | |
x.report("Iterative - Bill: ") {iterative_search("Bill", array_to_search)} | |
x.report("Binary - Bob: ") {binary_search("Bob", array_to_search)} | |
x.report("Iterative - Bob: ") {iterative_search("Bob", array_to_search)} | |
x.report("Binary - Joe: ") {binary_search("Joe", array_to_search)} | |
x.report("Iterative - Joe: ") {iterative_search("Joe", array_to_search)} | |
x.report("Binary - Sally: ") {binary_search("Sally", array_to_search)} | |
x.report("Iterative - Sally: ") {iterative_search("Sally", array_to_search)} | |
x.report("Binary - Sussie: ") {binary_search("Sussie", array_to_search)} | |
x.report("Iterative - Sussie: ") {iterative_search("Sussie", array_to_search)} | |
x.report("Binary - None: ") {binary_search("None", array_to_search)} | |
x.report("Iterative - None: ") {iterative_search("None", array_to_search)} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment