English 中文(简体)
如果执行得更频繁,那么再实施法典就会更长时间?
原标题:Same code takes longer if executed more often?

I ve got the following code inside a <script> tag on a webpage with nothing else on it. I m afraid I do not presently have it online. As you can see, it adds up all primes under two million, in two different ways, and calculates how long it took on average. The variable howOften is used to do this a number of times so you can average it out. What puzzles me is, for howOften == 1, method 2 is faster, but for howOften == 10, method 1 is. The difference is significant and holds even if you hit F5 a couple of times.

我的问题是:如何来?

(这一员额已经编辑,以纳入现成建议。) 但是,这并没有改变! 我现在非常pu。

(再一次:howOften>> 或1000多处,时间似乎稳定。) 答案似乎正确。

function methodOne(maxN) {
    var sum, primes_inv, i, j;

    sum = 0;
    primes_inv = [];
    for ( var i = 2; i < maxN; ++i ) {
        if ( primes_inv[i] == undefined ) {
            sum += i;
            for ( var j = i; j < maxN; j += i ) {
                primes_inv[j] = true;
            }
        }
    }
    return sum;
}

function methodTwo(maxN) {
    var i, j, p, sum, ps, n;

    n = ((maxN - 2) / 2);
    sum = n * (n + 2);
    ps = [];
    for(i = 1; i <= n; i++) {
        for(j = i; j <= n; j++) {
            p =  i + j + 2 * i * j;
            if(p <= n) {
                if(ps[p] == undefined) {
                    sum -= p * 2 + 1;
                    ps[p] = true;
                }
            }
            else {
                break;
            }
        }
    }
    return sum + 2;
}



// ---------- parameters
var howOften = 10;
var maxN = 10000;

console.log( iterations:  , howOften);
console.log( maxN:  , maxN);


// ---------- dry runs for warm-up
for( i = 0; i < 1000; i++ ) {
    sum = methodOne(maxN);
    sum = methodTwo(maxN);
}

// ---------- method one
var start = (new Date).getTime();

for( i = 0; i < howOften; i++ )
    sum = methodOne(maxN);

var stop = (new Date).getTime();
console.log( methodOne:  , (stop - start) / howOften);

// ---------- method two

for( i = 0; i < howOften; i++ )
    sum = methodTwo(maxN);

var stop2 = (new Date).getTime();
console.log( methodTwo:  , (stop2 - stop) / howOften);
最佳回答

简而言之,联合材料运行时间是优化的联合技术汇编者。 也就是说,对一行,对您的代码作了解释(tint),然后汇编成册(tjit),最后由您编辑成册(trun)。

现在,你计算得最多的是(tint+t<>subjit+trun)/N。 鉴于几乎靠Ntrun<>run <>/sub>的那部分情况,这一比较令人遗憾地没有意义。

因此,我不知道答案。 找到适当的答案

  1. Extract the code you are trying to profile into functions
  2. Run warm-up cycles on these functions, and do not use timing from the warm-up cycles
  3. Run much more than 1..10 times, both for warm-up and measurement
  4. Try swapping the order in which you measure time for algorithms
  5. Get into JS interpretator internals if you can and make sure you understand what happens: do you really measure what you think you do? Is JIT run during the warm-up cycles and not while you measure? Etc., etc.

<>Update:还指出,在1个周期中,你的时间少于系统时间的分辨率,这意味着错误可能大于你比较的实际价值。

问题回答

方法 有两个简单要求加工商进行较少的计算。 在方法上,你最初的休息时间是最长的。 在计算方法时,你最初的休息时间是执行(最高为1-2倍)。 因此,在第二种方法中,加工商的计算量不到第一种方法的计算量的一半。 每一种方法都含有一种nes状,使情况更加复杂。 因此,方法一大,是最大2。 避孕器具(最大-2)2





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签