-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Julia: update benchmark code #178
base: main
Are you sure you want to change the base?
Conversation
Moving numbers to the type-level allows Julia to effectively memoize via its dispatch system and constant propagation. Removing this implicit memoization makes the implementation match the others in the benchmark. As proof, calling "@code_native"" on "fibonacci(Val(10))" gives the results below with the result value of 55 memoized: .text .file "fibonacci" .globl julia_fibonacci_9798 # -- Begin function julia_fibonacci_9798 .p2align 4, 0x90 .type julia_fibonacci_9798,@function julia_fibonacci_9798: # @julia_fibonacci_9798 ; Function Signature: fibonacci(Base.Val{10}) ; ┌ @ In[21]:7 within `fibonacci` \# %bb.0: # %top push rbp mov rbp, rsp mov eax, 55 pop rbp ret .Lfunc_end0: .size julia_fibonacci_9798, .Lfunc_end0-julia_fibonacci_9798 ; └ # -- End function .section ".note.GNU-stack","",@progbits
Encapsulating the code in a function allows it to be compiled. The basic compilation unit in Julia is a function, so any code at the top level is not compiled. "@inbounds" macros were removed as they didn't make a difference for performance in this case. All other changes were stylistic to make the code more idiomatic.
fibonacci(::Val{1}) = 1 | ||
# general case | ||
fibonacci(::Val{n}) where n = fibonacci(Val(n-1)) + fibonacci(Val(n-2)) | ||
fibonacci(n) = n < 2 ? n : fibonacci(n-1) + fibonacci(n-2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to go back to having an if / case for 0 and 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is (n == 0 || no == 1) ? n : fibonacci(n-1) + fibonacci(n-2)
okay, or does it need to be the full
n == 0 ? 0 :
n == 1 ? 1 :
fibonacci(n-1) + fibonacci(n-2)
main(u) = println(sum(fibonacci, 1:u)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain if / how this is equivalent to the loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sum
function just calls mapreduce
with addition as the reducing function. So this is equivalent to mapping fibonacci
to all elements in the range from 1 to u
and then adding them up.
If you're prioritizing making the implementations more uniform between languages rather than using more idiomatic language constructs, going back to a loop is just fine. Let me know.
Please see each commit message for justification.