Replies: 2 comments
-
I have a question: are dicts and structs even comparable? |
Beta Was this translation helpful? Give feedback.
0 replies
-
It was to compare bigger Data Types that are "mutable", in our case if we should use one or another to store some data such as program counter , gas , etc.. |
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 Updating Struct vs Inserting in Dict: A Benchmark
Introduction
This report compares the gas cost of maintaining and changing values on a struct against inserting new values into a dictionary (Dict) in Cairo. The tests were conducted using scarb 0.6.0-alpha.2 and cairo 2.1.0-rc2.
Obs: For this test, both initialization of the struct and the dict were added on the tests (Dict initialization cost 15420 gas, and struct initialization didn't change the gas)
Methodology
The benchmarks were conducted under the following configurations:
Overall Result
The comparison highlights a significant difference in gas consumption:
```
Test with Updating Struct
Description
This approach involves maintaining and changing values on a struct. Each change on the struct is performed 20 times.
Code
Result
Gas consumption for this test was 57470.
Test with Inserting in Dict
Description
This approach involves inserting new values into a dictionary (Dict). Each insertion is performed 20 times. An insert on Dict takes around 5000 gas units.
Code
Result
Gas consumption for this test was 131040.
Conclusion
The tests indicate that maintaining and changing values on a struct (57470 gas) is cheaper than inserting new values into a dictionary (131040 gas) in the given setup and conditions. This result highlights the potential efficiency gains of using structs when handling multiple changes in Cairo.
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