Created
February 27, 2019 13:18
-
-
Save KristofferC/6b5ca57a58e3c7b8801eef0e08f8f71a to your computer and use it in GitHub Desktop.
This file contains 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
using BenchmarkTools | |
@enum Shape Rock Paper Scissors | |
function play(a::Shape, b::Shape) | |
if a == b | |
return 0 | |
elseif a == Paper && b == Rock | |
return 1 | |
elseif a == Paper && b == Scissors | |
return 2 | |
elseif a == Rock && b == Scissors | |
return 3 | |
else | |
return play(b, a) | |
end | |
end | |
getv() = rand([Paper, Rock, Scissors], 10_000) | |
A = getv() | |
B = getv() | |
@btime play.(A, B) |
This file contains 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
using BenchmarkTools | |
abstract type Shape end | |
struct Rock <: Shape end | |
struct Paper <: Shape end | |
struct Scissors <: Shape end | |
play(::Type{Paper}, ::Type{Rock}) = 1 | |
play(::Type{Paper}, ::Type{Scissors}) = 2 | |
play(::Type{Rock}, ::Type{Scissors}) = 3 | |
play(::Type{T}, ::Type{T}) where {T<: Shape} = 0 | |
play(a::Type{<:Shape}, b::Type{<:Shape}) = play(b, a) # Commutativity | |
getv() = rand([Paper, Rock, Scissors], 10_000) | |
A = getv() | |
B = getv() | |
@btime play.(A, B) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment