Java-script vs Groovy

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Groovy
Java:
m = null
def f(i) {
  if (m == null) m = new long[i+1]
  return m[i] = i<2?1:(m[i]!=0?m[i]:f(i-1) as long+f(i-2))
}
println f(args[0] as int);

Java-script
Java:
import static java.lang.System.*;
import static java.lang.Integer.*;
long[] m = null
long f(int i) {
  if (m == null) m = new long[i+1];
  return m[i]=i<2?1:(m[i]>0?m[i]:f(i-1)+f(i-2));
}
out.println(f(parseInt(getProperty("args"))))
exit(0)

Bash:
$ time ./fib.java 100
1298777728820984005

real    0m1.239s
user    0m2.772s
sys    0m0.257s
$ time ./fib.java 100
1298777728820984005

real    0m0.963s
user    0m2.853s
sys    0m0.176s
$ time ./fib.java 100
1298777728820984005

real    0m0.901s
user    0m2.598s
sys    0m0.171s
$ time ./fib.groovy 100
1298777728820984005

real    0m1.608s
user    0m4.288s
sys    0m0.372s
$ time ./fib.groovy 100
1298777728820984005

real    0m1.505s
user    0m4.553s
sys    0m0.345s
$ time ./fib.groovy 100
1298777728820984005

real    0m1.476s
user    0m4.486s
sys    0m0.326s
$

If you are observant, you will be asking the right questions.
:)
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,719
Reaction score
529
I am not WhiteAnt. Pardon me for gate crashing. ;)
Don't programming language need clarity? Groovy seems cryptic. Might as well go back to assembly.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
I am not WhiteAnt. Pardon me for gate crashing. ;)
Don't programming language need clarity? Groovy seems cryptic. Might as well go back to assembly.

It is fine, you are welcome to enquire.
No worries @WhiteAnt is just static noise whom think given the world this big, good S/W engineers only exist in FAANG. Surely I don't call myself top notch, but if I'm that bad, I'm not so sure if there is any space for him/her to stand. :cool:

How is Groovy cryptic? Care to explain?
It is one of the early dynamic language that work on top of JVM

Compared to Perl, would you still call Groovy more cryptic?

You should be one of those in this forum whom can appreciate really cryptic Perl code like this ?
Perl:
use bigint;

sub f {
  my $i = shift;
  $m[$i]=$i<2?1:$m[$i]?$m[$i]:f($i-1)+f($i-2);
}

print f(100),"\n";

Groovy in comparison is nothing. In Perl, one can put in implicitness that only those that knows Perl can decipher and understand. So Groovy in comparison is really quite simple as long as you understand it's syntax and semantics.

In any case, this thread is not about Groovy. Groovy is just a comparison. You should look at how I execute the Java codes. That is also related to how I wrote the Title.

:)
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top