You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Comparing Module Function vs Struct Function: A Benchmark
Introduction
This report compares the gas cost of calling a function from a module (mod) against calling a function from a struct in Cairo. The tests were conducted using scarb 0.6.0-alpha.2 and cairo 2.1.0-rc2.
Methodology
The benchmarks were conducted under the following configurations:
Language: Cairo
Version: scarb 0.6.0-alpha.2, cairo 2.1.0-rc2
Operations: Calling addition function from mod and struct
Metrics: Gas consumption
Overall Result
Overall it seems the difference is basically on initializing the Struct, the cost of purely calling the functions was the same. (The current 180 gas difference is due to the line of initializing a Struct)
The comparison indicates no significant difference in gas consumption:
| Approach | Gas Consumption |
|-------------------------|-----------------|
| Module Function | 7120 |
| Struct Function | 7300 |
Test with Module Function
Description
This approach involves calling an addition function from a module, performing the addition operation twice.
This approach involves calling an addition function from a struct, performing the addition operation twice.
Code
#[derive(Drop)]
structTest_Struct {}
traitTestStructTrait {
fnnew() ->Test_Struct;
/// Create a new instance of the EVM instructions.fnadd(refself:Test_Struct, a:u32, b:u32) ->u32;
}
implTestStructImplofTestStructTrait{
fnnew() ->Test_Struct{
Test_Struct {}
}
fnadd(refself:Test_Struct, a:u32, b:u32) ->u32{
a+b
}
}
#[test]
#[available_gas(100000)]
fntest_struct_functions(){
letx=testing::get_available_gas();
gas::withdraw_gas().unwrap();
letmuttest_struct=TestStructTrait::new();
leta=1;
letb=9;
letc=test_struct.add(a,b);
letd=test_struct.add(a,b);
'------Struct Function Add---'.print();
// 7300 gas
(x-testing::get_available_gas()).print();
}
Result
Gas consumption for this test was 7300.
Conclusion
The tests indicate that there is no significant difference between calling a mod function vs calling a function from a struct in Cairo for the given setup and conditions. The observation might lead to further exploration in evaluating the differences in deployment of struct format vs mod format functions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Comparing Module Function vs Struct Function: A Benchmark
Introduction
This report compares the gas cost of calling a function from a module (mod) against calling a function from a struct in Cairo. The tests were conducted using scarb 0.6.0-alpha.2 and cairo 2.1.0-rc2.
Methodology
The benchmarks were conducted under the following configurations:
Overall Result
Overall it seems the difference is basically on initializing the Struct, the cost of purely calling the functions was the same. (The current 180 gas difference is due to the line of initializing a Struct)
The comparison indicates no significant difference in gas consumption:
Test with Module Function
Description
This approach involves calling an addition function from a module, performing the addition operation twice.
Code
Result
Gas consumption for this test was 7120.
Test with Struct Function
Description
This approach involves calling an addition function from a struct, performing the addition operation twice.
Code
Result
Gas consumption for this test was 7300.
Conclusion
The tests indicate that there is no significant difference between calling a mod function vs calling a function from a struct in Cairo for the given setup and conditions. The observation might lead to further exploration in evaluating the differences in deployment of struct format vs mod format functions.
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