首页 > 其他分享 >[VM] Deopt code

[VM] Deopt code

时间:2023-11-06 15:33:17浏览次数:24  
标签:COUNT function code benchmark VM value export Deopt

const COUNT = Number.parseInt(process.argv[2] || "10");
console.log(`Running ${COUNT} iterations.`);

let value = 0;
export function benchA() {
  value = value === 0 ? 0 : 1;
}
export function benchB() {
  value = value === 0 ? 0 : 2;
}
export function benchC() {
  value = value === 0 ? 0 : 3;
}
export function benchD() {
  value = value === 0 ? 0 : 4;
}

//benchmark('-------------------------- IGNORE --------------------------', benchA);

if (require.main === module) {
  benchmark("A", benchA);
  benchmark("B", benchB);
  benchmark("C", benchC);
  benchmark("D", benchD);
}

/////////////////////

function benchmark(name: string, fn: () => void) {
  console.log("Starting:", name, "...");
  const start = performance.now();
  for (let i = 0; i < COUNT; i++) {
    fn();
  }
  const duration = performance.now() - start;
  console.log(
    "         ",
    name,
    Number((duration / COUNT) * 1000 * 1000).toFixed(3),
    "us"
  );
}

 

For the code above, we got result that benchmark("A", benchA);runs twice as faster than others.

The reason is that modern CPU and Javascript engines helps to optimize the code. It tries to do Branch predicition: it sees that value === 0 ? 0 : 1happens everytimes and it is easily predicatable. 

This allow CPU to inlnie the function to make it run faster

for (let i = 0; i < COUNT; i++) {
    // fn();  // before inline
    value = 0 // after inline
  }

 

But when it comes to run B,C, D benchmark, JS engine see the assumption it made before was wrong, now the has changed. So that it performe deopt operation :

for (let i = 0; i < COUNT; i++) {
    // value = 0 // inline
    fn();  // deopt
  }

that's why, in the end, benchmark A run faster than B,C & D.

 

标签:COUNT,function,code,benchmark,VM,value,export,Deopt
From: https://www.cnblogs.com/Answer1215/p/17812814.html

相关文章

  • nvm实现对nodejs的版本管理
    下载Releases·nvm-sh/nvm·GitHub介绍在我们的日常开发中经常会遇到这种情况:手上有好几个项目,每个项目的需求不同,进而不同项目必须依赖不同版的NodeJS运行环境。如果没有一个合适的工具,这个问题将非常棘手nvm应运而生,nvm是Mac下的node管理工具,有点类似管理Ruby的rvm,如果......
  • (五)Python之PVM介绍
    PVM介绍PVM(pythonvirtualmachine)Python解释器执行Python代码的时候,经历如下几个阶段:1)加载代码文件2)翻译成AST(语法分析所获得的中间结果)3)生成bytecode4)在PVM(pythonvirtualmachine)上执行byecode,PVM实际是一个基于栈的虚拟机......
  • [Leetcode] 0118. 杨辉三角
    118.杨辉三角题目描述给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。 示例1:输入:numRows=5输出:[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]示例 2:输入:numRows=1输出:[[1]] 提......
  • Educational Codeforces Round 157 (Rated for Div. 2)
    A.TreasureChest题目大意:人在0处,宝藏在x,钥匙在y,人最多拿宝箱z秒,问你最快多久开宝箱?思路:如果说钥匙在宝箱的左边,那么人只需要往右走就是最佳答案,如果钥匙在宝箱的右边,那么人只需要拿的宝箱到最佳地点就行#include<bits/stdc++.h>usingnamespacestd;voidsolve(){ intx,y......
  • VMware Workstation 15 Pro 安装macOS 10.15 Catalina (cdr文件版)
    VMwareWorkstation15Pro安装macOS10.15Catalina(cdr文件版) 所需工具:1.VMwareworkstation15Pro2.Unlocker (https://github.com/paolo-projects/unlocker/releases)https://github.com/paolo-projects/unlocker/releases/download/3.0.3/unlocker.zip3.macOScdr安......
  • Educational Codeforces Round 157 (Rated for Div. 2)
    F.FancyArrays第一眼感觉是去容斥掉条件1,因为条件2其实挺紧的。不妨用\(f(l,r)\)表示\(a\)值域在\([l,r]\)的方案(满足条件2)。那么答案为\(f(0,+\infty)-f(0,x-1)-f(x+k,+\infty)\),因为如果选了\([0,x-1]\)的数,那么还要更大的话,一定会选到\([x,x+k-1]\),所以你要......
  • Codeforces Round 907 (Div. 2)
    ASortingwithTwos题目大意:选择一个m,然后将1~2^m下表的数减一,可以操作无限次,问你能不能使数组单调递增题目数据851234556534496557566874432162245328131719275717913531757179921012340678910YESYESYE......
  • LeetCode 精选100题-70题爬楼梯
    题目描述:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?思路:第一阶楼梯:n=1,有一种方法f(1)=1;第二阶楼梯:n=2,有两种方法f(2)=2;当我们第一步爬了1个台阶时,我们可以有f(n-1)种方法爬到楼顶;当我们第一步爬了2......
  • Vscode 安装pyside6
    1.在VScode扩展中安装pyqtintegration2.pyqtintegration设置pyrcc;pyuic;Qtdesigner的路径 ......
  • Code Aesthetic
    01AbstractionAbstractionistheprocessofaggregatingcodewithhighsimilarityamongmultipleclassesintooneclasstoachievecommongoals.Theideaof"identifyingrepetitionandextractingitout"willmakeusgetintothemodeof"......