[go: up one dir, main page]

Skip to content

Instantly share code, notes, and snippets.

@MASDXI
Last active August 23, 2024 15:37
Show Gist options
  • Save MASDXI/5047e967a7444acf3acb129a25daca7b to your computer and use it in GitHub Desktop.
Save MASDXI/5047e967a7444acf3acb129a25daca7b to your computer and use it in GitHub Desktop.
Many style of 'Loop' in Solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0 <0.9.0;
// @author sirawt
contract LoopPerformance {
uint256 public sum;
function clear() public {
sum=0;
}
// Naive For
function NaiveFor(uint256 n) public returns (uint256) {
for (uint256 i = 0; i <= n; i++) {
sum +=i;
}
return sum;
}
// Naive For Unchecked
function NaiveForUnchecked(uint256 n) public returns (uint256) {
unchecked {
for (uint256 i = 0; i <= n; i++) {
sum +=i;
}
}
return sum;
}
// Reverse For
function ReverseFor(uint256 n) public returns (uint256) {
for (uint256 i = n; i > 0; i--) {
sum +=i;
}
return sum;
}
// Reverse For Unchecked
function ReverseForUnchecked(uint256 n) public returns (uint256) {
unchecked {
for (uint256 i = n; i > 0; i--) {
sum +=i;
}
}
return sum;
}
// Reverse While
function ReverseWhile(uint256 n) public returns (uint256) {
while(n > 0) {
sum += n;
n--;
}
return sum;
}
// Reverse While Uncheck
function ReverseWhileUnchecked(uint256 n) public returns (uint256) {
unchecked{
while(n > 0) {
sum += n;
n--;
}
}
return sum;
}
// Naive For Array
function NaiveForArr() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
for (uint256 i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
// Naive For Unchecked Array
function NaiveArrForUnchecked() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
unchecked {
for (uint256 i = 0; i < arr.length; i++) {
sum += arr[i];
}
}
return sum;
}
// Reverse For Array
function ReverseForArr() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
sum += arr[0];
for (uint256 i = arr.length-1; i > 0; i--) {
sum += arr[i];
}
return sum;
}
// Reverse For Unchecked Array
function ReverseForUncheckedArr() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
sum += arr[0]; // normal
unchecked {
// sum += arr[0]; // cheating?
for (uint256 i = arr.length-1; i > 0; i--) {
sum += arr[i];
}
}
return sum;
}
// Reverse While Array
function ReverseWhileArr() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
sum += arr[0];
uint256 i = arr.length -1;
while(i > 0) {
sum += arr[i];
i--;
}
return sum;
}
// Reverse While Unchecked Array
function ReverseWhileUncheckedArr() public returns (uint256) {
uint8[24] memory arr =
[2,3,76,187,78,101,204,202,229,247,103,192,164,106,149,75,153,160,9,97,253,128,82,92];
sum += arr[0]; // normal
unchecked {
// sum += arr[0]; // cheating?
uint256 i = arr.length - 1;
while(i > 0) {
sum += arr[i];
i--;
}
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment