Is there a way to time how long a function takes? - apache-flex

I have some function I want to time, but I have no idea how to do it. In javascript I can just get the current time in milliseconds, run the rest of my function, get the current time in milliseconds again and alert the difference. Viola, I know how long the function ran.
In ActionScript, it runs everything at once, so my start and end times are the same. How can I measure the span of time a function is taking to process?
Thanks,

Quick way below. It's better to do a statistical test, eg. run the toMeasure a 100 times between setting time1 and time2 and dividing the result by 100. It might give you a more realistic estimate. Remember that modern computers can do a bit of calculations in under a millisecond.
private var time1:Number;
private var time2:Number;
private function toMeasure():void
{
for (var i:int = 0;i<30000;i++)
{
trace (i);
}
}
protected function main():void
{
time1= new Date().time;
toMeasure();
time2= new Date().time;
trace ("ms:"+(time2-time1));
}

Related

Alternative to moveWithCollision

I've been making a (potentially massive) multiplayer video game in javascript, nodejs and babylonjs...
It's been a few months that a code is causing me problems in front:
it's currently a simple first person shooter with waves of monsters in player versus player.
The code I'm having trouble with is gravity.
players and monsters can therefore fall or climb, and those from more or less high, of course, the ground is not smooth.
for the moment I calculate the gravity in front only (the server does not receive "y"): I would like not to change that.
The monsters, which can be numerous, are also impacted by gravity only in front.
I searched, went through the babylonjs api and I can't find a viable alternative to "moveWithCollisions"
if (gamers[id][0] == false) {
var before = scene.getMeshByName(gamers[id][1]).gravity.y;
var after = scene.getMeshByName(gamers[id][1]).gravity.y;
for (var a = 0; a < Math.abs(4); a++) {
if (Math.round(after) == Math.round(before)) {
before = scene.getMeshByName(gamers[id][1]).position.y+scene.getMeshByName(gamers[id][1]).gravity.y;
scene.getMeshByName(gamers[id][1]).moveWithCollisions(scene.getMeshByName(gamers[id][1]).gravity);
after = scene.getMeshByName(gamers[id][1]).position.y;
} else {
a = 5;
gamers[id][0] = true;
}
}
when the character receives a move gamers[id][0] becomes false again
I really need a lighter command:
With 100 players and 1000 monsters you will understand that there is no chance that this code will work?
Hence my question, not having the strength to create this command myself, is there a real alternative to the "moveWithCollision" command?
I take you to consider my question, I have been looking for several weeks without finding anything better...
thanks in advance

Overriding a conditional variable in UnrealScript with a child class in Deus Ex?

I'm actually using the original Deus Ex game from the year 2000. I created a child class of "DeusEx.Flare" and called it "rflare" and saved it to my own package. I have successfully compiled it, and it works, but not the way I intended. I want to override the function "LifeSpan = 30" and give it "LifeSpan = 120". The problem is the documentation. There practically is none. And the documentation I can find generally is too confusing and does not give good enough examples for what I'm tryng to do. here is the code. I know I'm supposed to be using the "super" expression but I have exhausted all the ways I know how to use it. I simply cannot get it to work. However, I can get it to work if I dont' mind throwing both the normal flare (which goes out in 30 seconds) and my own flare, which only drops to the ground without a sound but will in fact last 120 seconds. So my code would end up throwing 2 flares. 1 normal flare that goes out in 30 sec. and the other that does last 120 but does not get thrown like the normal flare does.
here is the code from DeusEx.Flare script that I'm trying to change.
function LightFlare()
{
local Vector X, Y, Z, dropVect;
local Pawn P;
if (gen == None)
{
LifeSpan = 30;
}
}
My first attempt was to copy this and change it in my own package. It worked but again, it shot 2 flares, 1 normal and 1 that sorta worked. I want to do only one. So here is my attempt at correcting the code.
function LightFlare()
{
Super(Flare).LightFlare();
if (gen == None)
{
LifeSpan = 120;
}
}
All this does is spawn the normal flare, with no difference in the time it lasts. Can someone please help me?
I would suggest copying the LightFlare function in it's entirety from the parent class and not calling super. You don't want the original function to run, as that will mess with the lifetime variable.
For instance:
class RFlare extends Flare;
function LightFlare()
{
local Vector X, Y, Z, dropVect;
local Pawn P;
// Original function here, change lifetime when specified.
if (gen == None)
{
LifeSpan = 120;
}
}
defaultproperties
{
}

Flink: enrich a data set with a new column based on some computation

I am trying to do a simple processing with a data set.
Consider a data set with two columns of type String. To this data set I want to add a third column of type Long, which accumulates the number of records so far seen in the data set.
Example:
Input:
a,b
b,c
c,d
Output:
a,b,1
b,c,2
c,d,3
I have tried the following solution but I get a strange result:
DataSet<Tuple2<String, String>> csvInput = env.readCsvFile("src/main/resources/data_file")
.ignoreFirstLine()
.includeFields("11")
.types(String.class,String.class);
long cnt=0;
DataSet<Tuple3<String, String, Long>> csvOut2 = csvInput.map(new MyMapFunction(cnt));
private static class MyMapFunction implements MapFunction<Tuple2<String, String>, Tuple3<String, String, Long>> {
long cnt;
public MyMappingFunction(long cnt) {
this.cnt = cnt;
}
#Override
public Tuple3<String, String, Long> map(Tuple2<String, String> m) throws Exception {
Tuple3 <String ,String, Long> resultTuple = new Tuple3(m.f0,m.f1, Long.valueOf(cnt));
cnt++;
return resultTuple;
}
}
When I apply this solution for a file with 100 entries I get a count of 47 instead of 100. The counter is restarted at 53. Similarly, when I apply it for even a larger file the counter is somehow reset from time to time so I don't get the total number of the lines.
Could you please explain why is my implementation behaving in this way? Also, what could be a possible solution to my problem?
Thanks!
This is a multithreading issue. How many tasks slots do you have?
I had to clean up your code before running it - I suggest posting full working examples in future so that you have a chance of getting more answers.
The way you are keeping track of the count is not thread-safe, and so if you have more than one task slot you will have problems with the count value being inaccurate.
The proper way to count, as shown in the data artisans word count example, would be to use the 3rd slot in your tuple to simply store the value 1, and then sum the dataset.
resultTuple = new Tuple3(m.f0,m.f1, 1L);
then
csvOut2.sum(2).print();
where 2 is the index of the tuple containing the value 1.

Geb: Waiting/sleeping between tests

Is there a way to wait a set amount of time between tests? I need a solution to compensate for server lag. When creating a record, it takes a little bit of time before the record is searchable in my environment.
In the following code example, how would I wait 30 seconds between the first test and the second test and have no wait time between second test and third test?
class MySpec extends GebReportingSpec {
// First Test
def "should create a record named myRecord"() {
given:
to CreateRecordsPage
when:
name_field = "myRecord"
and:
saveButton.click()
then:
at IndexPage
}
// Second Test
def "should find record named myRecord"() {
given:
to SearchPage
when:
search_query = "myRecord"
and:
searchButton.click()
then:
// haven't figured this part out yet, but would look for "myRecord" on the results page
}
// Third Test
def "should delete the record named myRecord"() {
// do the delete
}
}
You probably don't want to wait a set amount of time - it will make your tests slow. You would ideally want to continue as soon as the record is added. You can use Geb's waitFor {} to poll for a condition to be fulfilled.
// Second Test
def "should find record named myRecord"() {
when:
to SearchPage
then:
waitFor(30) {
search_query = "myRecord"
searchButton.click()
//verify that the record was found
}
}
This will poll every half a second for 30 seconds for the condition to be fulfilled passing as soon as it is and failing if it's still not fulfilled after 30 seconds.
To see what options you have for setting waiting time and interval have look at section on waiting in The Book of Geb. You might also want to check out the section on implicit assertions in waitFor blocks.
If your second feature method depends on success of the first one then you should probably consider annotating this specification with #Stepwise.
You should always try to use waitFor and check conditions wherever possible. However if you find there isn't a specific element you can check for, or any other condition to check, you can use this to wait for a specified amount of time:
def sleepForNSeconds(int n) {
def originalMilliseconds = System.currentTimeMillis()
waitFor(n + 1, 0.5) {
(System.currentTimeMillis() - originalMilliseconds) > (n * 1000)
}
}
I had to use this while waiting for some chart library animations to complete before capturing a screenshot in a report.
Thread.sleep(30000)
also does the trick. Of course still agree to "use waitFor whenever possible".

Total Time reports with XtraScheduler control

Is any easy way to make "total time" reports with XtraScheduler control.
I need couple reports that will calculate total time by day/week/month for each resorce and appointment type.
Is posible to do this with XtraSchedulerReport or I must calculate this manualy?
There aren't any built in functions to calculate the total time by resource and appointment type, but it's definitely possible to do this using XtraScheduler. Check out the two following examples for better details on how:
http://www.devexpress.com/Support/Center/e/E977.aspx
http://www.devexpress.com/Support/Center/e/E121.aspx
:)
Also, try something like this:
internal static TimeIntervalCollectionEx CalculateBusyTime(AppointmentBaseCollection appointments, TimeInterval interval) {
TimeIntervalCollectionEx busyIntervals = new TimeIntervalCollectionEx();
int count = appointments.Count;
for (int i = 0; i < count; i++)
busyIntervals.Add(TimeInterval.Intersect(interval, appointments[i].CreateInterval()));
return busyIntervals;
}

Resources