Optimizer 参数设置,gas如何变化?

optimizer-runs 200 是 Solidity 编译器的一个选项,用于指定优化器的运行次数。优化器是 Solidity 编译器的一部分,它可以对编译后的代码进行优化,以提高代码的执行效率。--optimizer-runs 200 意味着优化器将运行200次,以尝试找到最优的代码优化方案。

--via-ir 是 Solidity 编译器的另一个选项,用于指定编译器是否应该使用中间代码(IR)来编译 Solidity 代码。IR 是一种中间表示形式,它可以将 Solidity 代码转换为一种更容易优化和转换的形式。使用 IR 可以提高编译器的效率,并且可以生成更高效的代码。如果指定了 --via-ir 选项,则编译器将使用 IR 来编译 Solidity 代码。

以下是实际测试实例:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;


contract MyContract {
    address public owner;
    uint256 public number;

    constructor() {
        owner = msg.sender;
    }

    function sum(uint256 _value) external {
        require(owner == msg.sender);
        number = number + _value;
    }
}


// NO OPTIMIZER
// Deployment cost: 234,980    223364--158546
// Function call cost: 46, 063 46048--24844
// OPTIMIZER SET TO 200 RUNS
// Deployment cost: 141,686     139877--81443
// Function call cost: 45, 724  45726--24522
// OPTIMIZER SET TO 10, 000 RUNS
// Deployment cost: 158,684     156875--97655
// Function call cost: 45, 712  45714--24510

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值