Menu

[r459]: / trunk / php-java-bridge / examples / bench / bench2.php  Maximize  Restore  History

Download this file

89 lines (75 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/php
<?php
if (!extension_loaded('java')) {
if (!(include_once("java/Java.php"))&&!(PHP_SHLIB_SUFFIX=="so" && dl('java.so'))&&!(PHP_SHLIB_SUFFIX=="dll" && dl('php_java.dll'))) {
echo "java extension not installed.";
exit(2);
}
}
$n=200000;
$Sys = new JavaClass("java.lang.System");
$a1 = array();
for($i=0; $i<$n; $i++) {
$a1[$i]=$i;
}
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
$v = new java("java.util.LinkedList", $a1);
$t2 = $Sys->currentTimeMillis();
$T1 = $t2-$t1;
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
$v = new java("java.util.HashMap", $a1);
$t2 = $Sys->currentTimeMillis();
$T2 = $t2-$t1;
$Arrays=new JavaClass("java.util.Arrays");
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
$v = $Arrays->asList($a1);
$t2 = $Sys->currentTimeMillis();
$T3 = $t2-$t1;
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
java_begin_document();
$v = new java("java.util.LinkedList");
for($i=0; $i<$n; $i++) {
$v->add($i, $i);
}
java_end_document();
$t2 = $Sys->currentTimeMillis();
$T4 = $t2-$t1;
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
java_begin_document();
$v = new java("java.util.HashMap");
for($i=0; $i<$n; $i++) {
$v->put($i, $i);
}
java_end_document();
$t2 = $Sys->currentTimeMillis();
$T5 = $t2-$t1;
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
java_begin_document();
$Array = new JavaClass("java.lang.reflect.Array");
$ar=$Array->newInstance(new JavaClass("java.lang.Integer"), $n);
for($i=0; $i<$n; $i++) {
$Array->set($ar, $i, $i);
}
java_end_document();
$t2 = $Sys->currentTimeMillis();
$T6 = $t2-$t1;
$s="";
for($i=0; $i<$n; $i++) {
$s.=$i;
}
$Sys->gc();
$t1 = $Sys->currentTimeMillis();
$str = new java("java.lang.String", $s);
$t2 = $Sys->currentTimeMillis();
$T7 = $t2-$t1;
echo "Time needed to send $n values to the server.\n";
echo "constructor : LinkedList: $T1 ms, HashMap: $T2 ms, Array: $T3 ms\n";
echo "invocations : LinkedList: $T4 ms, HashMap: $T5 ms, Array: $T6 ms\n";
echo "Sending a {$str->length()} length string: $T7 ms\n";
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.