Replies: 1 comment
-
would that make a difference if you had return statements in each branch? Here, you're not really comparing two similar behaviors. |
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
-
Comparing Multiple Consecutive
if
vsif-else
Statements: A BenchmarkIntroduction
This report compares the gas cost of using multiple consecutive
if
statements versus usingif-else
statements. The tests were conducted considering 20 conditions, utilizing scarb 0.6.0-alpha.2 and cairo 2.1.0-rc2.Methodology
The benchmarks were conducted under the following configurations:
Overall Result
The comparison between consecutive 20 consecutive if statements and 20 consecutive if-else statements highlight a significant difference in gas consumption:
Test with Consecutive
if
StatementsDescription
This approach uses consecutive
if
statements, where each condition is evaluated independently.Code
Result
Gas consumption for this test was 7620.
Test with if-else Statements
Description
This approach uses if-else statements, evaluating conditions sequentially and exiting when a true condition is met.
Code
Result
Gas consumption for this test was 7620.
Conclusion
The tests indicate that using if-else costs the same amount of steps compared to consecutive ifs.
Appendix:
Versions used:
scarb: 0.6.0-alpha.2 (4b4d5411b 2023-07-25)
cairo: 2.1.0-rc2 (https://crates.io/crates/cairo-lang-compiler/2.1.0-rc2)
Beta Was this translation helpful? Give feedback.
All reactions