Parzh from Ukraine
Parzh from Ukraine

Reputation: 9903

Do I got number of operations per second in this way?

Look at this code:

function wait(time) {
    let i = 0;
    let a = Date.now();
    let x = a + (time || 0);
    let b;
    while ((b = Date.now()) <= x) ++i;
    return i;
}

If I run it in browser (particularly Google Chrome, but I don't think it matters) in the way like wait(1000), the machine will obviously freeze for a second and then return recalculated value of i.
Let it be 10 000 000 (I'm getting values close to this one). This value varies every time, so lets take an average number.

Did I just got current number of operations per second of the processor in my machine?

Upvotes: 0

Views: 2578

Answers (4)

Pedro Ferrari
Pedro Ferrari

Reputation: 323

There is a lot of truth to all previous comments, but I want to invert the reasoning a little bit because I do believe it is easier to understand it like that.

I believe that the fairest way to calculate it is with the most basic loop, and not relying on any dates or functions, and instead calculate the values later. You will see that the smaller the function, the bigger the initial overload is. That means it takes a small amount of time to start and finish each function, but at a certain point they all start reaching a number that can clearly be seen as close-enough to be considered how many operations per second can JavaScript run.

My example:

const oneMillion        =         1_000_000;
const tenMillion        =        10_000_000;
const oneHundredMillion =       100_000_000;
const oneBillion        =     1_000_000_000;
const tenBillion        =    10_000_000_000;
const oneHundredBillion =   100_000_000_000;
const oneTrillion       = 1_000_000_000_000;

function runABunchOfTimes(times) {
    console.time('timer')
    for (let i = 0; i < times; ++i) {}
    console.timeEnd('timer')
}

I've tried on a machine that has a lot of load already on it with many processes running, 2020 macbook, these were my results:

javascript operations per second

at the very end I am taking the time the console showed me it took to run, and I divided the number of runs by it. The oneTrillion and oneBillion runs are virtually the same, however when it goes to oneMillion and 1000 you can see that they are not as performant due to the initial load of creating the for loop in the first place.

We usually try to sway away from O(n^2) and slower functions exactly because we do not want to reach for that maximum. If you were to perform a find inside of a map for an array with all cities in the world (around 10_000 according to google, I haven't counted) we would already each 100_000_000 iterations, and they would certainly not be as simple as just iterating through nothing like in my example. Your code then would take minutes to run, but I am sure you are aware of this and that is why you posted the question in the first place.

Calculating how long it would take is tricky not only because of the above, but also because you cannot predict which device will run your function. Nowadays I can open in my TV, my watch, a raspberry py and none of them would be nearly as fast as the computer I am running from when creating these functions. Sure. But if I were to try to benchmark a device I would use something like the function above since it is the simplest loop operation I could think of.

Upvotes: 1

Travis Smith
Travis Smith

Reputation: 542

As others have pointed out, this will not help you determine the number of operations the processor does per second due to the factors that prior answers have pointed out. I do however think that a similar experiment could be set up to estimate the number of operations to be executed by your JavaScript interpreter running on your browser. For example given a function: factorial(n) an operation that runs in O(n). You could execute an operation such as factorial(100) repeatedly over the course of a minute.

function test(){
  let start = Date.now();
  let end = start + 60 * 1000;
  let numberOfExecutions = 0;
  while(Date.now() < end){
     factorial(100);
     numberOfExecutions++;
  }
  return numberOfExecutions/(60 * 100);
}

The idea here is that factorial is by far the most time consuming function in the code. And since factorial runs in O(n) we know factorial(100) is approximately 100 operations. Note that this will not be exact and that larger numbers will make for better approximations. Also remember that this will estimate the number of operations executed by your interpreter and not your processor.

Upvotes: 0

salezica
salezica

Reputation: 77029

Not at all.

What you get is the number of loop cycles completed by the Javascript process in a certain time. Each loop cycle consists of:

  • Creating a new Date object
  • Comparing two Date objects
  • Incrementing a Number

Incrementing the Number variable i is probably the least expensive of these, so the function is not really reporting how much it takes to make the increment.

Aside from that, note that the machine is doing a lot more than running a Javascript process. You will see interference from all sorts of activity going on in the computer at the same time.

When running inside a Javascript process, you're simply too far away from the processor (in terms of software layers) to make that measurement. Beneath Javascript, there's the browser and the operating system, each of which can (and will) make decisions that affect this result.

Upvotes: 2

Corvus Crypto
Corvus Crypto

Reputation: 2291

No. You can get the number of language operations per second, though the actual number of machine operations per second on a whole processor is more complicated.

Firstly the processor is not wholly dedicated to the browser, so it is actually likely switching back and forth between prioritized processes. On top of that memory access is obscured and the processor uses extra operations to manage memory (page flushing, etc.) and this is not gonna be very transparent to you at a given time. On top of that physical properties means that the real clock rate of the processor is dynamic... You can see it's pretty complicated already ;)

To really calculate the number of machine operations per second you need to measure the clock rate of the processor and multiply it by the number of instructions per cycle the processor can perform. Again this varies, but really the manufacturer specs will likely be good enough of an estimate :P.

If you wanted to use a program to measure this, you'd need to somehow dedicate 100% of the processor to your program and have it run a predictable set of instructions with no other hangups (like memory management). Then you need to include the number of instructions it takes to load the program instructions into the code caches. This is not really feasible however.

Upvotes: 1

Related Questions