Recent

Author Topic: FP vs Java Benchmark  (Read 28845 times)

Jay

  • New Member
  • *
  • Posts: 31
FP vs Java Benchmark
« on: December 05, 2016, 01:27:29 pm »
I'm currently learning Java as well as FP & Lazarus and was curious about the performance of Java compared to FP so thought I'd compare the times to find 100,000 primes. Code for FP:

Code: Pascal  [Select][+][-]
  1. program primes;
  2. uses sysutils, dateutils;
  3. const
  4.    LIMIT = 100000;
  5. var
  6.   i, j : longint;
  7.   isPrime : boolean;
  8.   t1, t2 : tdatetime;
  9. begin
  10.   {start timer}
  11.   t1 := now;
  12.   for i := 1 to LIMIT do begin
  13.     isPrime := true;
  14.     for j := 2 to i-1 do
  15.       if i mod j = 0 then begin
  16.         isPrime := false;
  17.         break;
  18.       end;             
  19.   end;
  20.   {stop timer}
  21.   t2 := now;
  22.   writeln('Time taken: ', millisecondsbetween(t1, t2));
  23. end.

The times taken over 10 runs were the following:

7492
7500
7519
7536
7475
7489
7493
7471
7531
7511

Now the Java code (using version 1.8 of the JRE)
Code: Javascript  [Select][+][-]
  1. public class Primes {
  2.   public static void main(String[] args) {
  3.     int limit = 100000;
  4.     // start timer
  5.     long start = System.currentTimeMillis();
  6.     for(int i = 1; i < limit; i++) {
  7.       boolean isPrime = true;
  8.       for(int j = 2; j < i; j++) {
  9.         if(i % j == 0) {
  10.           isPrime = false;
  11.           break;
  12.         }
  13.       }
  14.     }
  15.     // stop timer
  16.     long stop = System.currentTimeMillis();
  17.     System.out.println("Time taken: " + (stop - start));
  18.   }
  19. }

And the times:

7005
7270
7685
6845
7153
6865
6913
7998
6859
7119

There was quite a bit more variation in these times than for FP, but on average:

Time taken for FP :  7501.7 ms
Time taken for Java : 7171.2 ms

I was expecting Java to be significantly slower than FP.  I know you shouldn't read too much into these kinds of tests, but an interesting result nevertheless.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: FP vs Java Benchmark
« Reply #1 on: December 05, 2016, 01:33:08 pm »
This is pretty much wellknown. If you program Java to a very low, almost  procedural level in an overviewable size,  the VM can actually make its claims come true.

The performance degrations come from not this basic level but primarily from higher level usage (wrapping it in classes that might need extra memory allocated) and the memory allocation strategy (GC) in general.

To be honest, I'm surprised that Java doesn't smoke FPC, a multi billion company against a handful of hobbyists ?

Jay

  • New Member
  • *
  • Posts: 31
Re: FP vs Java Benchmark
« Reply #2 on: December 05, 2016, 01:50:29 pm »
To be honest, I'm surprised that Java doesn't smoke FPC, a multi billion company against a handful of hobbyists ?

The FP developers have done a phenomenal job.  :)

Plus, FP is more general purpose than Java, which isn't much good for really low-level stuff. And I prefer FP syntax (much more readable). Of course FP also wins easily on memory usage.

Having said that, the popularity of Java means there are a lot of resources so maybe it's easier for a newbie programmer to get started with.

derek.john.evans

  • Guest
Re: FP vs Java Benchmark
« Reply #3 on: December 05, 2016, 02:09:22 pm »
Just to clarify.

Was the FP test run outside of Lazarus. ie: Without a debugger present?

What FP optimization level was used?

Ive always viewed Java vs Native code based on the speed of apps produced. Java app's have always felt awful to me. C# app's are a little better.

I've always preferred well written native code app's

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: FP vs Java Benchmark
« Reply #4 on: December 05, 2016, 02:10:57 pm »
Having said that, the popularity of Java means there are a lot of resources so maybe it's easier for a newbie programmer to get started with.

I always considered Java horribly beginner unfriendly. You need a lot of boilerplate code and concepts for a minimal program.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: FP vs Java Benchmark
« Reply #5 on: December 05, 2016, 02:39:31 pm »
I like Java, the only problem like with C# is that you must:
- distribute the runtime stuff (java, net dll) with your program
- tell the user to download the runtime stuff (well, with C# some comes pre installed with Windows or with automatic updates)

With FPC you just distribute a binary.

About the speed, with current PC performance there is no reason (at least for the kind of programs I do) to focus 100% on speed, if Java produces more or less than FPC is not the main reason to choose one or the other.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: FP vs Java Benchmark
« Reply #6 on: December 05, 2016, 03:26:13 pm »
Funny.   >:D 8-) O:-)
FPC is just as fast ;)

Code: Pascal  [Select][+][-]
  1. program primes;
  2. //compiled with:
  3. //  ppcjvm -Fu/usr/local/lib/fpc/3.1.1/units/jvm-java/rtl/* primes.pas
  4. //run as:
  5. //  java -cp /usr/local/lib/fpc/3.1.1/units/jvm-java/rtl:. primes
  6.  
  7. {$MACRO ON}
  8. {$Define Now := jlsystem.currentTimeMillis}
  9. {$Define Write := JlSystem.fOut.Print}
  10. {$Define WriteLn := JLSystem.fOut.PrintLn}
  11. uses jdk15;
  12. const
  13.    LIMIT = 100000;
  14. var
  15.   i, j : longint;
  16.   isPrime : boolean;
  17.   t1, t2 : Longint;
  18.   s:string;
  19. begin
  20.   {start timer}
  21.   t1 := Now;
  22.   for i := 1 to LIMIT do begin
  23.     isPrime := true;
  24.     for j := 2 to i-1 do
  25.       if i mod j = 0 then begin
  26.         isPrime := false;
  27.         break;
  28.       end;            
  29.   end;
  30.   {stop timer}
  31.   t2 := Now;
  32.   Write('Time taken: ');
  33.   WriteLn( t2 - t1);
  34. end.
  35.  

Now what?  :(
« Last Edit: December 05, 2016, 03:37:37 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Jay

  • New Member
  • *
  • Posts: 31
Re: FP vs Java Benchmark
« Reply #7 on: December 05, 2016, 03:45:53 pm »
Was the FP test run outside of Lazarus. ie: Without a debugger present?

What FP optimization level was used?

I didn't use any switches (and no debugger), but recompiling with -O3 optimisation cut down the time to around 6600 ms.  :)

Jay

  • New Member
  • *
  • Posts: 31
Re: FP vs Java Benchmark
« Reply #8 on: December 05, 2016, 03:54:30 pm »
I like Java, the only problem like with C# is that you must:
- distribute the runtime stuff (java, net dll) with your program
- tell the user to download the runtime stuff (well, with C# some comes pre installed with Windows or with automatic updates)

Doesn't the JRE come preinstalled in Windows? I haven't used it for years so I don't know, but I imagine if you try to run a java application the OS will download it for you.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: FP vs Java Benchmark
« Reply #9 on: December 05, 2016, 04:15:45 pm »
[Doesn't the JRE come preinstalled in Windows? I haven't used it for years so I don't know, but I imagine if you try to run a java application the OS will download it for you.

Nope. You have to obtain it from java,com
Note some OEM distributions that come with your PC or laptop come with Java pre-installed because their bloat-ware/unwanted-ware needs it.
« Last Edit: December 05, 2016, 04:19:13 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: FP vs Java Benchmark
« Reply #10 on: December 05, 2016, 04:30:12 pm »
Note on my Raspberrypi 3 the ppcjvm java code from my example above runs twice as fast as the native arm code with all optimizations on!
So it also depends on platform:
---------
pi@raspberrypi:~ $ ./primes_arm
Time taken: 22464
pi@raspberrypi:~ $ java -cp /usr/local/lib/fpc/3.1.1/units/jvm-java/rtl:. primes
Time taken: 11085
« Last Edit: December 05, 2016, 04:33:35 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

hrayon

  • Full Member
  • ***
  • Posts: 118
Re: FP vs Java Benchmark
« Reply #11 on: December 05, 2016, 05:42:27 pm »
Java in controlled environments should be very good, but in accounting offices is a hell  >:D. When you upgrade the java version, one or other program stops working. You have to keep upgrading and downgrading for the various programs to work  %). The Federal Revenue from Brazil adopted java in several software, and for me it was a big mistake. Some bank sites also need the updated java to function properly. The end of java support in Google Chrome confirms the error in choosing java in these cases.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: FP vs Java Benchmark
« Reply #12 on: December 05, 2016, 05:45:49 pm »
Well, you can always bundle a specific java version for each software, nowdays 80 mb is not too much for TB hard disks.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: FP vs Java Benchmark
« Reply #13 on: December 05, 2016, 05:50:56 pm »
Well, you can always bundle a specific java version for each software, nowdays 80 mb is not too much for TB hard disks.
Huh? With the security history of Java? Of course not! Always keep up-to-date and don't expose Java code to the outside world.
Keep it running on a dedicated server and ask on demand, plastered with certificates for every type of connection on the intra-net. And behind a trump-like mexican firewalled fence.

Trust me, been there done that, saw the accidents happen that were waiting to happen.

That said: Java can do things - thanks to its eco system - that are not easily achievable otherwise. Specifically in, say, a banking environment.
Just don't expose it to the internet. Let it perform its tasks and export just the generated data afterwards.
« Last Edit: December 05, 2016, 05:55:26 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: FP vs Java Benchmark
« Reply #14 on: December 05, 2016, 05:56:54 pm »
Thanks for your reply, I was not thinking on that. And why they add support to make an installer like that in NetBeans? It bundles the java runtime plus your application, you must do that for each platform. They think that you will always pack the application with the latest java?

 

TinyPortal © 2005-2018