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.
This needs to go back to having an if / case for 0 and 1.
There was a problem hiding this comment.
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)| end | ||
| println(r) | ||
| end No newline at end of file | ||
| main(u) = println(sum(fibonacci, 1:u)) |
There was a problem hiding this comment.
Can you explain if / how this is equivalent to the loop?
There was a problem hiding this comment.
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.
|
I'd love for Julia to join the list of languages that are represented in the new in-process benchmarks.
Julia was one of my reasons to insist on shifting to benchmarking without start-times involved. The new reference fibonacci programs do |
Please see each commit message for justification.