This repository was archived by the owner on Jan 8, 2025. It is now read-only.
Replies: 1 comment
-
|
@danilowhk Could you link the Sierra programs to each of these implementations? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Comparing Loop vs Recursion: A Benchmark
Introduction
This report compares the gas cost of using a loop against using recursion for performing addition operations in Cairo. The tests were conducted using scarb 0.6.0-alpha.2 and cairo 2.1.0-rc2.
Overview
It seems there is no difference if both loop and recursion are as a function, but if the loop is programmed within the function / test, there are performance gains
Methodology
The benchmarks were conducted under the following configurations:
Overall Result
The comparison highlights a difference in gas consumption:
Test with Recursion
Description
This approach uses a recursive function to perform 20 addition operations.
Code
Result
Gas consumption for this test was 54020.
Test with Loop As Function
Description
This approach uses a separate loop function to perform 20 addition operations.
Code
Result
Gas consumption for this test was 54020.
Test with Loop
Description
This approach uses a loop inside the function to perform 20 addition operations.
Code
Result
Gas consumption for this test was 54020.
Conclusion
The tests indicate that all three approaches are equally gas-efficient (54020 gas) for performing 20 addition operations in Cairo. The result highlights the similarity in gas consumption between recursion, loop inside the function, and loop as a function.
Appendix:
Versions used:
scarb 0.6.2 (c07fa6155 2023-08-15)
cairo: 2.1.1 (https://crates.io/crates/cairo-lang-compiler/2.1.1)
Beta Was this translation helpful? Give feedback.
All reactions