Skip to content

Instantly share code, notes, and snippets.

@samusz
Forked from KristofferC/enums.jl
Created June 26, 2019 21:54
Show Gist options
  • Save samusz/ff53e0a51028112db8180000212f9033 to your computer and use it in GitHub Desktop.
Save samusz/ff53e0a51028112db8180000212f9033 to your computer and use it in GitHub Desktop.
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)
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