Skip to main content

computation too slow

5 replies [Last post]
rabinovic
Offline
Joined: 2007-12-02
Points: 0

after compiling and running the following code you will notice that the computation takes a lot of time comparing with java or other languages. Why??

import java.lang.System;

var N:Integer = 500;

var found:Boolean[] = [for(i in [1..N]) [false]];
var cardcnt = 0;
var valcnt = 0;

while (valcnt < N) {
var val1:Number = (Math.random()*N);
var val:Integer = val1.intValue();

cardcnt++;

if (not found[val]) {
valcnt++;
}

found[val] = true;
}

System.out.println(cardcnt);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tball
Offline
Joined: 2003-06-10
Points: 0

It doesn't make sense to compare tests which have random input. Try using java.util.Random with the same seed (say, 123456789L), so that test runs are the same.

rabinovic
Offline
Joined: 2007-12-02
Points: 0

sorry but it is still too slow

bi11w00ds
Offline
Joined: 2003-07-02
Points: 0

> sorry but it is still too slow

I had to modify the code you had so it could run (see below) and also utilized the built in javaFX println object and it seems to run fairly fast for me. Here is the console output.

........
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 0.031
3792
init: 0.625
........

Are you sure you've downloaded the latest jars etc.?
Also did you do this in JavaFXPad?
I ran it stand alone as a DOS batch process.
------------------------------------------- CODE --------------------------------------------------------
import java.lang.Math;

var N:Integer = 500;

var found:Boolean = foreach(i in [1..N])[false];

var cardcnt = 0;
var valcnt = 0;

while (valcnt < N) {
var val1:Number = (Math.random()*N);
var val:Integer = val1.intValue();

cardcnt++;

if (not found[val]) {
valcnt++;
}
foundit[val] -> true; //replace -> with equal sign before running
}

println(cardcnt);
--------------------------------------------------------------------------------------------------------------

rabinovic
Offline
Joined: 2007-12-02
Points: 0

try it with the jfx-compiler (not interpreted version!) and tell me if it is fast. thanks

rabinovic
Offline
Joined: 2007-12-02
Points: 0

here is the rest:
...
found[val] = true;
};

System.out.println(cardcnt);