Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大数相加问题以及jsPerf性能测试报告 #88

Open
Chenjiayuan195 opened this issue May 25, 2018 · 1 comment
Open

大数相加问题以及jsPerf性能测试报告 #88

Chenjiayuan195 opened this issue May 25, 2018 · 1 comment

Comments

@Chenjiayuan195
Copy link

Chenjiayuan195 commented May 25, 2018

https://jsperf.com/calc-compare-twice/
这一份是对比了两次while循环的substr,发现我的单次while循环基本上比它慢一点

https://jsperf.com/calc-compare-bit/
这一份是对比按位操作的,基本上单次循环的substr比它快

我的方法基本上处于两者之间,为保证测试结果测试用例参考的是issue中的,想请问一下老师,我的写法是否有改进的地方
ps:不知道为何我的jsperf无法添加snippet,没有那个按钮=。=

@Chenjiayuan195
Copy link
Author

Chenjiayuan195 commented May 25, 2018

const myadd = (x, y) => {
let sum=[],
    res=0,
    lenMax=0,
    rs,
    X,
    Y,
    Xlen,
    Ylen,
    rsLen;
while (x.length > 0 || y.length > 0) {
  X=x.substr(-14);
  Y=y.substr(-14);
  Xlen=X.length,
  Ylen=Y.length;
  rs=String(Number(X)+Number(Y)+res);
  lenMax=Math.max(Xlen,Ylen);
  rsLen=rs.length;
  if(rsLen>lenMax){
    rs=rs.substr(1,rsLen-1);
    res=1;
  }else{
    res=0;
  }
  x = x.substr(0, x.length - 14);
  y = y.substr(0, y.length - 14);
  sum.unshift(rs)
}
res==1&&sum.unshift(res)
return sum.join('');
}

附上我的代码

@miniflycn miniflycn reopened this Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants