<script>
var a=new Date();
console.log(a.getSeconds());
for(var b=0;b<1000;b++){
console.log("b:"+b)
}
console.log(a.getSeconds());
</script>
为什么这样重复打印,A的时间没有发生变化。。?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你已经把执行这条语句
var a=new Date();时的时间存放在a这个变量中了。如果不改变a,它的值始终不会变。要想获得执行函数花费的时间,你可以这样做:意思是执行一条语句不需要一毫秒?
看执行时间的啊,我试过偶尔会有1毫秒的差距的
这种方法可以用来测试函数的性能,不过一毫秒以内就无法测出来了
如果需要统计耗时,可以这么做:
console.time('xxx');
for(var b=0;b<1000;b++){
}
console.timeEnd('xxx');
// 输出: xxx: 0.03ms