https://handwiki.org/wiki/index.php?title=DMelt:Programming/Benchmarks&feed=atom&action=historyDMelt:Programming/Benchmarks - Revision history2024-03-28T13:45:57ZRevision history for this page on the wikiMediaWiki 1.38.4https://handwiki.org/wiki/index.php?title=DMelt:Programming/Benchmarks&diff=327&oldid=previmported>Jworkorg at 16:03, 14 February 20212021-02-14T16:03:53Z<p></p>
<p><b>New page</b></p><div><br />
<br />
{{sidebar box|[[DMelt:Start|Table of contents]]}}<br />
<br />
<br />
== Benchmarking supported languages ==<br />
<br />
You should make a smart choice what program language you should use for your project.<br />
If you are doing a lot of numeric computations and want to use scripting, use Groovy. <br />
<br />
<jnote><br />
Groovy is a factor 10 faster than the standard Python or JRuby. The latter has a similar speed as the Python).<br />
</jnote><br />
<br />
Jython and BeanShell can only be used if you manipulate with high-level libraries. Another choice is to implement code in Java, make a library, and call it from Jython and BeanShell.<br />
<br />
Let us illustrate the speed of the executions. I'll benchmark a Monte Carlo calculation of PI (3.14159) by throwing "darts". <br />
The calculations are implemented in Java, Groovy, BeanShell, JRuby, Jython and Python (Python2 implemented in C).<br />
<br />
Here is the original Java code "MonteCarloPI.java":<br />
<br />
<jcode lang="java"><br />
public class MonteCarloPI {<br />
public static void main(String[] args)<br />
{<br />
int nThrows = 0;<br />
int nSuccess = 0;<br />
double x, y;<br />
long then = System.nanoTime();<br />
int events=(int)1e8;<br />
for (int i = 0; i < events; i++) {<br />
x = Math.random(); // Throw a dart<br />
y = Math.random();<br />
nThrows++;<br />
if ( x*x + y*y <= 1 ) nSuccess++;<br />
}<br />
int itime = (int)((System.nanoTime() - then)/1e9);<br />
System.out.println("Time for calculations (sec): " + itime+"\n");<br />
System.out.println("Pi = " + 4*(double)nSuccess/(double)nThrows +"\n");<br />
}<br />
}<br />
</jcode><br />
<br />
Here is a Groovy code put to a file "MonteCarloPI.groovy":<br />
<br />
<jcode lang="java"><br />
int nThrows = 0; int nSuccess = 0;<br />
double x, y;<br />
long then = System.nanoTime();<br />
int events=(int)1e8;<br />
for (int i = 0; i < events; i++) {<br />
x = Math.random(); y = Math.random(); // Throw a dart <br />
nThrows++;<br />
if ( x*x + y*y <= 1 ) nSuccess++;<br />
}<br />
int itime = (int)((System.nanoTime() - then)/1e9);<br />
System.out.println("Time for calculations (sec): " + itime+"\n");<br />
System.out.println("Pi = " + 4*(float)nSuccess/(float)nThrows +"\n");<br />
</jcode><br />
<br />
Here is the JRuby code "MonteCarloPI.rb":<br />
<br />
<jcode lang="ruby"><br />
require "java"<br />
java_import java.lang.Math;<br />
java_import java.lang.System;<br />
nThrows = 0; nSuccess = 0;<br />
xthen = System.nanoTime();<br />
events=1e8;<br />
for i in 0 .. events do<br />
x = Math.random(); y = Math.random(); # Throw a dart <br />
nThrows +=1<br />
if ( x*x + y*y <= 1 )<br />
nSuccess += 1<br />
end<br />
end<br />
itime = (System.nanoTime() - xthen)/1e9;<br />
xpi=(4.0*nSuccess)/nThrows<br />
puts "Time for calculations (sec): #{itime}"<br />
puts "Pi = #{xpi}"<br />
</jcode><br />
<br />
And here is a Jython code "MonteCarloPI.py":<br />
<br />
<jcode><br />
from java.util import Random<br />
from java.lang import *<br />
<br />
nThrows,nSuccess = 0,0<br />
then = System.nanoTime()<br />
events=int(1e8)<br />
for i in xrange(events):<br />
x,y = Math.random(),Math.random(); # Throw a dart <br />
nThrows +=1<br />
if ( x*x + y*y <= 1 ): nSuccess+=1<br />
itime = (int)((System.nanoTime() - then)/1e9)<br />
print "Time for calculations (sec): ",itime<br />
print "Pi = ", 4*nSuccess/float(nThrows)<br />
</jcode><br />
<br />
Note we use "xrange" instead of "range". The calculations that use "range" are very slow in Jython and cannot be used in this instance.<br />
<br />
I've renamed "MonteCarloPI.groovy" to a BeanShell script file "MonteCarloPI.bsh" (BeanShell has a very similar syntax as Groovy)<br />
<br />
In the case of the standard Python, the code is "MonteCarloPI_CPython.py" looks as this:<br />
<br />
<jcode><br />
import random<br />
import time<br />
<br />
nThrows,nSuccess = 0,0<br />
then = time.time()<br />
events=int(1e8)<br />
for i in range(events):<br />
x,y = random.random(),random.random(); # Throw a dart <br />
nThrows +=1<br />
if ( x*x + y*y <= 1 ): nSuccess+=1<br />
itime = time.time() - then<br />
print "Time for calculations (sec): ",itime<br />
print "Pi = ", 4*nSuccess/float(nThrows)<br />
</jcode><br />
<br />
Let us run "MonteCarloPI.java", "MonteCarloPI.groovy", "MonteCarloPI.py", MonteCarloPI.rb and "MonteCarloPI.bsh" inside DataMelt editor. <br />
Here is the benchmark results on a i7 x64 computer (Linux Mint), with 2048 MB allocated for JDK9 when running Groovy, Jython, BeanShell code:<br />
<br />
<jcode><br />
Java code: 3 sec Pi = 3.14176584 -> executed in DataMelt/JDK9<br />
Groovy code: 3 sec Pi = 3.14144832 -> executed in DataMelt/JDK9<br />
Python code: 3 sec Pi = 3.14188036 -> executed using PyPy<br />
Groovy code: 14 sec Pi = 3.14141132 -> when using loose types for x,y <br />
Python code: 28 sec Pi = 3.14188036 -> executed in Python (CPython)<br />
JRuby code: 31 sec Pi = 3.14187860 -> executed in DataMelt/JDK9<br />
Jython code: 40 sec Pi = 3.14187860 -> executed in DataMelt/JDK9<br />
BeanShell code: takes forever?! -> executed in DataMelt/JDK9<br />
</jcode><br />
<br />
As you can see, Java and Groovy calculations take about the same time (3 sec). Python and JRuby is a factor 10 slower than Java and Groovy.</div>imported>Jworkorg