Jump to content

Javascript Experiment


vytas

Recommended Posts

Hello i am busy with an project and i need some help. At this site visit the websiteThere are two javascript files: "gameEngine.js" & "organism.js" this one isn't needed. In the gameEngine there a method called runLoop(), and i want to know how many ms there is between each cycle sorta like a fps counter. But i have not a clue on how to do this properly so im requesting for some help.Thank you.

Link to comment
Share on other sites

If you want to calculate how many loops per second, keep a counter to increment every time through the loop. At the end of the loop, check the time and subtract it from the start time, if the difference is greater than or equal to 1 second then the counter is the number of loops that ran during that second. You can reset the counter and start time and continue counting for the next second.

var elapsed = d2.getTime() - d2.getTime();
It's so fast you can't even measure it!
Link to comment
Share on other sites

Oops! Copy and paste'll get me every time.var elapsed = d2.getTime() - d1.getTime(); // in millisecondsBut if the process is fast, you may need to set up something artificial, so run your loop 1000 times or 100,000 times, and then divide accordingly.

Link to comment
Share on other sites

thats really cool. I love the creations people can make when given a collection of tools.theres such hope in human ingenuity however, I'm not sure what applications knowing that would have. perhaps comparing it to another code that does the same thing?milliseconds do add up. I know he was checking the run time of a loop and not a function. but i just realize that its possible to have a function that finds run times of functions. it would be useful if you have to check the run Times of a multitude of functions I did it on the fly so it might have errors but something like this:

function runTime(func){D1 = new Date().getTime()func()D2 = new Date().getTime()runningTime = D2 - D1;return runningTime;}

it'll be interesting to pass runTime into itself. I don't believe it would ever reach D2...any of them.

Link to comment
Share on other sites

I first thought of dividing the engine cycles with the time running in ms. This would give me an average of how many cycles it makes in a ms. Multiply that by 1000 and u have the average engine cycles per seconds. I think il hold on to this idea, because it is easy. But i need to make sure the engine doesn't start running when it hasn't started. Else the average is completely messed up. And because this is one of my first times that i have worked with the OOP of javascript it isn't making this easyer. Il go and work out some of your ideas and see if something works.

Link to comment
Share on other sites

I'm not sure what applications knowing that would have. perhaps comparing it to another code that does the same thing?
That's exactly it. The technique is called benchmarking, and it is useful when you need to compare the speed of built-in techniques, but also techniques you create yourself. Here's a more realistic version -- what I was trying to describe above:
function runTime(func, num){var D1 = new Date().getTime(); for (var i = 0; i < num; i++) {	  func();} var D2 = new Date().getTime(); var runningTime = (D2 - D1) / num; return runningTime; }

The point is that a small bit of code may execute in a millisecond or less, so you couldn't get its speed in the normal way. But if you told the script to run it 1000 times, you might learn something.A benchmarking experiment: Routine X creates a page element (like a text input) using all DOM methods: createElement(), setAttribute() (className, id, name), appendChild(), and so on; make sure the element gets drawn to the page. Routine Y creates the same element using innerHTML. Run each one 100 times. See which is faster.

Link to comment
Share on other sites

it'll be interesting to pass runTime into itself.
It would generate an error the second time through because the function isn't checking if the argument was passed, it just tries to execute it. You'd get an error that func is undefined.
Link to comment
Share on other sites

Here's a bit of an offtopic question but how do you like my project? P.S. i made the FPS counter to work. I use 2 variables:

var FPS;var lastTime = new Date.getTime();

At the end of the loop i will do this:

FPS = (new Date().getTime() - lastTime) / 1000;lastTime = new Date().getTime();

Now i know how many times the engine runs per second which is quite interesting.

Link to comment
Share on other sites

I like it :) i think it'll be cool to have it display how many 'players' are black, white, or mixed. it reminds me of a fractal, in how i like to look at it.
It would require a bit more processing but it's possible.
Link to comment
Share on other sites

Any suggestions on how to improve this script?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...