Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active June 24, 2023 13:48
Show Gist options
  • Save IllIllI000/3dc79d25acccfa16dee4e83ffdc6ffde to your computer and use it in GitHub Desktop.
Save IllIllI000/3dc79d25acccfa16dee4e83ffdc6ffde to your computer and use it in GitHub Desktop.

GtVsGte_gt.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0; // optimization doesn't affect the results

/**
 * @title GtVsGte
 * @author IllIllI
 */
contract GtVsGte {
    function test (uint256 x, uint256 y) external pure returns(uint256) {
        if (x > y) {
	    return 1;
	}
	return 2;
    }
}

GtVsGte_gte.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0; // optimization doesn't affect the results

/**
 * @title GtVsGte
 * @author IllIllI
 */
contract GtVsGte {
    function test (uint256 x, uint256 y) external pure returns(uint256) {
	if (x >= y) {
	    return 1;
	}
	return 2;
    }
}

Diff

diff --git a/GtVsGte_gt.sol b/GtVsGte_gte.sol
index 50309c3..c07ceb8 100644
--- a/GtVsGte_gt.sol
+++ b/GtVsGte_gte.sol
@@ -7,7 +7,7 @@ pragma solidity >=0.6.0 <0.9.0; // optimization doesn't affect the results
  */
 contract GtVsGte {
     function test (uint256 x, uint256 y) external pure returns(uint256) {
-        if (x > y) {
+	if (x >= y) {
 	    return 1;
 	}
 	return 2;

Compiled binary

$ solc GtVsGte_gt.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060af8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b604d60048036036040811015604157600080fd5b5080359060200135605f565b60408051918252519081900360200190f35b600081831115606f575060016073565b5060025b9291505056fea2646970667358221220c3f6b0f074cae2518022c1a854471cca8000b99cdd41dad4a09a1339da8839e964736f6c63430006000033

$ solc GtVsGte_gte.sol --bin --optimize --optimize-runs 200 | egrep "^60[0-9]+"
608060405234801561001057600080fd5b5060ae8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063eb8ac92114602d575b600080fd5b604d60048036036040811015604157600080fd5b5080359060200135605f565b60408051918252519081900360200190f35b6000818310606e575060016072565b5060025b9291505056fea264697066735822122065ea8ceb174e6f74bd9b8dab9aeacf8d5d47e1aac38aa07d03ee03ea68824b8c64736f6c63430006000033

Opcodes

PC.Gt Operation.Gt Gas.Gt PC.Gte Operation.Gte Gas.Gte
129(0x81) DUP2(0x81) 3 129(0x81) DUP2(0x81) 3
130(0x82) DUP4(0x83) 3 130(0x82) DUP4(0x83) 3
131(0x83) LT(0x10) 3
131(0x83) GT(0x11) 3
132(0x84) ISZERO(0x15) 3
133(0x85) PUSH1(0x60) ["0x6f"] 3 132(0x84) PUSH1(0x60) ["0x6e"] 3
135(0x87) JUMPI(0x57) 10 134(0x86) JUMPI(0x57) 10
Total.Gt: 25 Total.Gte: 22

The change provides a savings of 3 gas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment