Arrays: Disappointed about ridiculous slow scala code compared to Java

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 - 3 | Next >

Arrays: Disappointed about ridiculous slow scala code compared to Java

by Burkhard Ludwig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

when posting microbenchmark results it would useful to adhere to the
rules of microbenchmarking as given here:

http://wikis.sun.com/display/HotSpotInternals/MicroBenchmarks

and the post command line or even all of the console output, so we can
assume the benchmark was done under a reasonable testing environment.

And yes, details are important, because in a long running application
the measured values may vary greatly e.g. between the server and
client JVM version (you used a client VM which is already a bad
sign...).

The inner loop seems really bogus to use reflection to access a
primitive array (all the boxing involved...). Though, I read somewhere
that reflective array access is really fast so there probably is a
rationale behind this compilation strategy. Let's wait what the more
informed people have to say...

Johannes

On Wed, Nov 4, 2009 at 11:29 AM, Burkhard Ludwig
<ludwig.burkhard@...> wrote:

> Good morning.
> Yesterday evening I wondered why some code of mine is so slow. Then I made
> an experiment.
> The bottom line is that the very same (simple) array based algorithm is
> ridiculous slow in scala vs. java and it got worse in 2.8.
> The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
> The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead
> of Array[Int] to a relation  java vs. scala = 1 : 1.3.
> So the question is: Is that expected? And is there an advice other than:
> Code your array stuff in plain Java ?
> I find it especially annoying, that the simple out-of-the-box solution using
> Array[T] is so painfully slow.
>
> As an experiment I used a dumb little shell sort algorithm which I coded in
> java and scala. This execises array lookup and assignments.
> The java and scala versions are appended below to give you an impression. (I
> save you from the complete test mock-up.)
> Timing measurements can be cristicised technically to death, and I did't try
> to set up a benchmark, but when you arrive at a factor of 22, then the
> details ar not important.
> Thank you for listening,
> Burkhard
> PS: A peek with javap -c showed something like the lines below in in the
> innermost loop of the plain 2.8-code, which
>       is totally absent in the plain java code and the generic array code
> has something like calls to apply and update.
>       So I fear that all this drag is by design, and that might well "tag"
> scala 2.8 as "fancy but so slooow".
>       I remembered the commercials: "Scala is almost as fast as java". Well
> thisis true unless you use fine new stuff
>       w/o recognizing.  I hope I am in error.
>    64: invokestatic #302; //Method
> java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
>   ...
>    74: invokestatic #306; //Method
> java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V
>
> PPS.: If anybody is interested, I can post the test mockups (which differ
> for 2.7 and 2.8)
> Scala versions used:
>  Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
>  scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM,
> Java 1.5.0_20)
> Run time comparisions
> The numbers are milliseconds for 100000 calls each. (gathered
> with System.currentTimeMillis)
> The data are for an empty measurement, an empty loop over the data, a simple
> reverse-code and shell- and insert_sort.
> both implemented in java and scala and for 2.8 also for GenericArrays.
> 1)
> Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java
> 1.5.0_20).
> Type in expressions to have them evaluated.
> Type :help for more information.
> scala> tst.Test_27.run(100000)
> empty measurement: 0
> empty loop: 8
> reverse: 113
> shell: 1482
> java shell: 538
> insert: 1608
> java insert: 544
> 2)
> Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM)
> Client VM, Java 1.5.0_20).
> Type in expressions to have them evaluated.
> Type :help for more information.
> scala> tst.Test_28.run(100000)
> empty measurement: 0
> empty loop: 7
> reverse: 1080
> shell: 13784
> java shell: 620
> generic shell: 828
> insert: 15288
> java insert: 630
> generic insert: 865
> Code examples
> Java:
> class Java<T> {
>
>   static <T> void shellSort(T[] a, int first, int last, Comparator<? super
> T> c) {
>     int h = (last - first)/3 +1;
>     while (h > 0) {
>       insert_sort(h, a, first, last, c);
>       h = h == 1 ? 0 : (h/3 + 1);
>     }
>   }
>   static <T> void insert_sort(int h, T[] a, int first, int last,
> Comparator<? super T> c) {
>     int i = first+h;
>     while (i < last) {
>       T pivot = a[i];
>       int j = i;
>       while (j >= h && c.compare(pivot, a[j-h]) < 0) {
>         a[j] = a[j-h];
>         j -= h;
>       }
>       a[j] = pivot;
>       i += h;
>     }
>   }
> }
> Scala
> final object Test_28 {
>   def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
>     var h = (last - first)/3 +1
>     while (h > 0) {
>       insert_sort(h, a, first, last)(c)
>       h = if (h == 1) 0 else h/3 + 1
>     }
>   }
>   def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c:
> Comparator[T]) {
>     var i = first+h
>     while (i < last) {
>       val pivot = a(i)
>       var j = i
>       while (j >= h && c.compare(pivot, a(j-h)) < 0) {
>         a(j) = a(j-h)
>         j -= h
>       }
>       a(j) = pivot
>       i += h
>     }
>   }
> }



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Ismael Juma :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Burkhard,

On Wed, 2009-11-04 at 11:29 +0100, Burkhard Ludwig wrote:

> Good morning.
>
> Yesterday evening I wondered why some code of mine is so slow. Then I
> made an experiment.
> The bottom line is that the very same (simple) array based algorithm
> is ridiculous slow in scala vs. java and it got worse in 2.8.
> The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 :
> 22,3.
> The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int]
> instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

The problem is that you're using polymorphic arrays. In Scala, it means
that it has to work with primitive arrays (int[], long[], etc.) as well
as boxed arrays (Object[], String[], etc.). The end result is a nasty
performance penalty. In Java, polymorphic arrays are sort of broken (try
new T[0]) and they don't try to unify primitives with boxed
representations so the performance penalty is not there.

> So the question is: Is that expected? And is there an advice other
> than: Code your array stuff in plain Java ?

In 2.7.x, the easiest solution is to use Array[AnyRef] or wrap an
Array[AnyRef] inside a generic collection (like ArrayBuffer does). This
should also work in 2.8.x, but you may be able to achieve better there
with manifests. I haven't tried it yet, so maybe someone else can expand
on this.

One main point is that Scala arrays should perform as well as Java
arrays if you're willing to write imperative-looking code and avoid
polymorphic arrays.

Best,
Ismael


Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There's no reflection calls in the code you posted (I compiled it here). A reflection call surely will make things slow, if it happens inside a loop. The only place left for it that would make a difference for performance is inside Comparator. Could you post that code (Java and Scala), please?

Other than that, Juma is correct in his observations with regards to parameterized arrays.

On Wed, Nov 4, 2009 at 8:29 AM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}



--
Daniel C. Sobral

Veni, vidi, veterni.

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Viktor Klang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sigh

I'm tired of performance envy involving arrays.
Arrays are a shallow abstraction over implementation detail (contigous memory).
They are also horridly implemented in the JVM (and in Java).

On Wed, Nov 4, 2009 at 12:23 PM, Daniel Sobral <dcsobral@...> wrote:
There's no reflection calls in the code you posted (I compiled it here). A reflection call surely will make things slow, if it happens inside a loop. The only place left for it that would make a difference for performance is inside Comparator. Could you post that code (Java and Scala), please?

Other than that, Juma is correct in his observations with regards to parameterized arrays.

On Wed, Nov 4, 2009 at 8:29 AM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}



--
Daniel C. Sobral

Veni, vidi, veterni.



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: viktorklang
Code: github.com/viktorklang

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Burkhard Ludwig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel,

please find my code appended.  The test setup is maybe somewhat backhanded.
It needs a lot of array duplication, since the test cases modify their input data.

You can either run it from the repl with "tst.Test_28.run(100000) or from the command line
scala tst.Test_28, which includes a warm-up phase.

The results differ, but the relation scala-2.8 to plain java remain unsatisfactory (using a server speedups mostly the plain java code)

What bothers me is that the results are bad when I do the most obvious (I think)

The test driver is scala-code, and all tests use the same comparator.
B.T.W:  generated code from which i posted the snippet was compiled with -optimise.
When I do not use -optimise, the code has about the same speed but use no calls to any reflection.

I hope it helps.

Burkhard


2009/11/4 Daniel Sobral <dcsobral@...>
There's no reflection calls in the code you posted (I compiled it here). A reflection call surely will make things slow, if it happens inside a loop. The only place left for it that would make a difference for performance is inside Comparator. Could you post that code (Java and Scala), please?

Other than that, Juma is correct in his observations with regards to parameterized arrays.

On Wed, Nov 4, 2009 at 8:29 AM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}



--
Daniel C. Sobral

Veni, vidi, veterni.




t.java (832 bytes) Download Attachment
t.scala (5K) Download Attachment

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Burkhard Ludwig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Victor,

> 2009/11/4 Viktor Klang <viktor.klang@...
> Sigh
> I'm tired of performance envy involving arrays.

Sorry but I did not understand this.

>  Arrays are a shallow abstraction over implementation detail (contigous memory).

I violently agree. But they are also part of "programming customs" of imperative guys, at least I use them if nothing more subtle is needed. And when they provide all the convenience that comes with the collections, I appreciate it, but they should not come with (big) surprises within the little world in which I move at a certain point. And, I might be mistaken, a glimpse to Java is within the little world of scala.

My point is not that I found some 30% penalty and do not want to accept that.
So what I would accept and not even recognize when a simple transfer of some algorithm from java would result in something like "0.5 Java ≤ Scala ≤ 2 Java" or so. This was my expectation about the positioning of scala with the realm of languages. If this expectation was met then I had some ground und the feet and could explore the scala world.

But I found really slow code and was surprised that is due to one of my everyday work-horses namely a shallow abstraction ..., which was slow by an order of magnitude. And  asked myself what to use instead.

So the original question was:

Is an order of magnitude of speed loss to be expected, and should I avoid what was for me the obvious namely write a

     "def f[T](a: Array[T], ...)"

and use it with an "Array[Int]"

So should change my habits go for whatever more fancy, and is it a bad idea to pull over Java algorithms and reuse them in scala?

Burkhard


2009/11/4 Viktor Klang <viktor.klang@...>
Sigh

I'm tired of performance envy involving arrays.
Arrays are a shallow abstraction over implementation detail (contigous memory).
They are also horridly implemented in the JVM (and in Java).


On Wed, Nov 4, 2009 at 12:23 PM, Daniel Sobral <dcsobral@...> wrote:
There's no reflection calls in the code you posted (I compiled it here). A reflection call surely will make things slow, if it happens inside a loop. The only place left for it that would make a difference for performance is inside Comparator. Could you post that code (Java and Scala), please?

Other than that, Juma is correct in his observations with regards to parameterized arrays.

On Wed, Nov 4, 2009 at 8:29 AM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}



--
Daniel C. Sobral

Veni, vidi, veterni.



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: viktorklang
Code: github.com/viktorklang


Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Viktor Klang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Burkhard,

On Wed, Nov 4, 2009 at 2:21 PM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Victor,

> 2009/11/4 Viktor Klang <viktor.klang@...
> Sigh
> I'm tired of performance envy involving arrays.

Sorry but I did not understand this.

If you're dealing with contigous memory manipulation, you'll have to decide what level of overhead is acceptable.
ASM, C, C++, Java, Scala, Javascript or whatnot.
 

>  Arrays are a shallow abstraction over implementation detail (contigous memory).

I violently agree. But they are also part of "programming customs" of imperative guys, at least I use them if nothing more subtle is needed.

Yes, and in that case, you should probably choose something more suitable, which varies on a case-to-case basis.
 
And when they provide all the convenience that comes with the collections, I appreciate it, but they should not come with (big) surprises within the little world in which I move at a certain point. And, I might be mistaken, a glimpse to Java is within the little world of scala.

Absolutely, and I know that Martin and the EFPL team are working hard on a high performance array implementation.
However, it's not a secret that elegance oftentimes comes with a price
see: http://www.google.se/search?q=Scala+Array+performance&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
 

My point is not that I found some 30% penalty and do not want to accept that.
So what I would accept and not even recognize when a simple transfer of some algorithm from java would result in something like "0.5 Java ≤ Scala ≤ 2 Java" or so. This was my expectation about the positioning of scala with the realm of languages. If this expectation was met then I had some ground und the feet and could explore the scala world.

But I found really slow code and was surprised that is due to one of my everyday work-horses namely a shallow abstraction ..., which was slow by an order of magnitude.

30% is not a magnitude.
 
And  asked myself what to use instead.

I missed that part, what is your use-case?

 

So the original question was:

Is an order of magnitude of speed loss to be expected, and should I avoid what was for me the obvious namely write a

     "def f[T](a: Array[T], ...)"

and use it with an "Array[Int]"

either use @specialized or Array[Int/Long] etc
 

So should change my habits go for whatever more fancy, and is it a bad idea to pull over Java algorithms and reuse them in scala?

What's the gain if they were to be pulled over?
If it's significant, do it, if not, YAGNI.



Cheers,

 

Burkhard


2009/11/4 Viktor Klang <viktor.klang@...>

Sigh

I'm tired of performance envy involving arrays.
Arrays are a shallow abstraction over implementation detail (contigous memory).
They are also horridly implemented in the JVM (and in Java).


On Wed, Nov 4, 2009 at 12:23 PM, Daniel Sobral <dcsobral@...> wrote:
There's no reflection calls in the code you posted (I compiled it here). A reflection call surely will make things slow, if it happens inside a loop. The only place left for it that would make a difference for performance is inside Comparator. Could you post that code (Java and Scala), please?

Other than that, Juma is correct in his observations with regards to parameterized arrays.

On Wed, Nov 4, 2009 at 8:29 AM, Burkhard Ludwig <ludwig.burkhard@...> wrote:
Good morning.

Yesterday evening I wondered why some code of mine is so slow. Then I made an experiment.
The bottom line is that the very same (simple) array based algorithm is ridiculous slow in scala vs. java and it got worse in 2.8.
The relation is as follows java : scala-2.7 : scala-2.8 = 1 : 2.6 : 22,3.
The 2.8-penalty can be reduced in scala 2-8 with GenericArray[Int] instead of Array[Int] to a relation  java vs. scala = 1 : 1.3.

So the question is: Is that expected? And is there an advice other than: Code your array stuff in plain Java ?
I find it especially annoying, that the simple out-of-the-box solution using Array[T] is so painfully slow.
 
As an experiment I used a dumb little shell sort algorithm which I coded in java and scala. This execises array lookup and assignments. 
The java and scala versions are appended below to give you an impression. (I save you from the complete test mock-up.)
Timing measurements can be cristicised technically to death, and I did't try to set up a benchmark, but when you arrive at a factor of 22, then the details ar not important.

Thank you for listening,

Burkhard

PS: A peek with javap -c showed something like the lines below in in the innermost loop of the plain 2.8-code, which
      is totally absent in the plain java code and the generic array code has something like calls to apply and update.
      So I fear that all this drag is by design, and that might well "tag" scala 2.8 as "fancy but so slooow". 
      I remembered the commercials: "Scala is almost as fast as java". Well thisis true unless you use fine new stuff
      w/o recognizing.  I hope I am in error.

   64: invokestatic #302; //Method java/lang/reflect/Array.get:(Ljava/lang/Object;I)Ljava/lang/Object;
  ...
   74: invokestatic #306; //Method java/lang/reflect/Array.set:(Ljava/lang/Object;ILjava/lang/Object;)V


PPS.: If anybody is interested, I can post the test mockups (which differ for 2.7 and 2.8)

Scala versions used:

 Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20). 
 scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20)

Run time comparisions

The numbers are milliseconds for 100000 calls each. (gathered with System.currentTimeMillis)
The data are for an empty measurement, an empty loop over the data, a simple reverse-code and shell- and insert_sort.
both implemented in java and scala and for 2.8 also for GenericArrays.

1) 

Welcome to Scala version 2.7.4.final (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_27.run(100000)
empty measurement: 0
empty loop: 8
reverse: 113
shell: 1482
java shell: 538
insert: 1608
java insert: 544

2)

Welcome to Scala version 2.8.0.r18925-b20091006020914 (Java HotSpot(TM) Client VM, Java 1.5.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> tst.Test_28.run(100000)
empty measurement: 0
empty loop: 7
reverse: 1080
shell: 13784
java shell: 620
generic shell: 828
insert: 15288
java insert: 630
generic insert: 865

Code examples

Java:

class Java<T> {
  
  static <T> void shellSort(T[] a, int first, int last, Comparator<? super T> c) {
    int h = (last - first)/3 +1;
    while (h > 0) {
      insert_sort(h, a, first, last, c);
      h = h == 1 ? 0 : (h/3 + 1);
    }
  }

  static <T> void insert_sort(int h, T[] a, int first, int last, Comparator<? super T> c) {
    int i = first+h;
    while (i < last) {
      T pivot = a[i];
      int j = i;
      while (j >= h && c.compare(pivot, a[j-h]) < 0) {
        a[j] = a[j-h];
        j -= h;
      }
      a[j] = pivot;
      i += h;
    }
  }
}

Scala

final object Test_28 {

  def shellSort[T](a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var h = (last - first)/3 +1
    while (h > 0) {
      insert_sort(h, a, first, last)(c) 
      h = if (h == 1) 0 else h/3 + 1
    }
  }

  def insert_sort[T](h: Int, a: Array[T], first: Int, last: Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }
}



--
Daniel C. Sobral

Veni, vidi, veterni.



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: viktorklang
Code: github.com/viktorklang




--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: viktorklang
Code: github.com/viktorklang

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Nils Kilden-Pedersen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 7:29 AM, Viktor Klang <viktor.klang@...> wrote:
either use @specialized

I thought @specialized was for class definitions only?
 
or Array[Int/Long] etc

Is that valid syntax?

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Burkhard Ludwig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Victor,

30% is not a magnitude.

A factor of 20 is and that was what I consistently observed, and I did write it. I just checked.
Sigh.

Well, thanks for the hint to @specialized. After I found out how to trigger it. it helped.
It brings the timings into a range where it belongs. So the rule of thumb is: When you use an Array[T], then don't forget to do a "def f[@specialize T](...)

Greetings,
Burkhard.

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 2:57 PM, Nils Kilden-Pedersen <nilskp@...> wrote:

> On Wed, Nov 4, 2009 at 7:29 AM, Viktor Klang <viktor.klang@...> wrote:
>>
>> either use @specialized
>
> I thought @specialized was for class definitions only?
>
>>
>> or Array[Int/Long] etc
>
> Is that valid syntax?
>
It works with
def insert_sort[@specialized T](h: Int, a: Array[T], first: Int, last:
Int)(c: Comparator[T])

so it is probably working on any type parameter definition.


--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

He meant "Array[Int] or Array[Long]".

2009/11/4 Nils Kilden-Pedersen <nilskp@...>:

> On Wed, Nov 4, 2009 at 7:29 AM, Viktor Klang <viktor.klang@...> wrote:
>>
>> either use @specialized
>
> I thought @specialized was for class definitions only?
>
>>
>> or Array[Int/Long] etc
>
> Is that valid syntax?
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...
Google Wave: ricky.clarkson@...

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 3:00 PM, Burkhard Ludwig
<ludwig.burkhard@...> wrote:

> Victor,
>
>> 30% is not a magnitude.
>
> A factor of 20 is and that was what I consistently observed, and I did write
> it. I just checked.
> Sigh.
> Well, thanks for the hint to @specialized. After I found out how to trigger
> it. it helped.
> It brings the timings into a range where it belongs. So the rule of thumb
> is: When you use an Array[T], then don't forget to do a "def f[@specialize
> T](...)
> Greetings,
> Burkhard.

Your benchmark is bogus. If you would use (<:AnyRef)

  def insert_sort[T<:AnyRef](h: Int, a: Array[T], first: Int, last:
Int)(c: Comparator[T]) {
    var i = first+h
    while (i < last) {
      val pivot = a(i)
      var j = i
      while (j >= h && c.compare(pivot, a(j-h)) < 0) {
        a(j) = a(j-h)
        j -= h
      }
      a(j) = pivot
      i += h
    }
  }

which would be equivalent to Javas definition

static <T> void insert_sort(int h, T[] a, int first, int last,
Comparator<? super T> c)

the compiled methods are almost equal, as well as the performance. The
underlying problem is that if you don't write the <:AnyRefs, there's
some boxing happening which in case of the Java example is done before
timing in the initialization phase.

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Nils Kilden-Pedersen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Damn, I though it might have been some cool new syntax, although I would have expected Array[Int | Long].

On Wed, Nov 4, 2009 at 8:03 AM, Ricky Clarkson <ricky.clarkson@...> wrote:
He meant "Array[Int] or Array[Long]".

2009/11/4 Nils Kilden-Pedersen <nilskp@...>:
> On Wed, Nov 4, 2009 at 7:29 AM, Viktor Klang <viktor.klang@...> wrote:
>>
>> either use @specialized
>
> I thought @specialized was for class definitions only?
>
>>
>> or Array[Int/Long] etc
>
> Is that valid syntax?
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...
Google Wave: ricky.clarkson@...


Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Array[Either[Int, Long]] works.  Though, yes, I'd prefer Either to be called |

2009/11/4 Nils Kilden-Pedersen <nilskp@...>:

> Damn, I though it might have been some cool new syntax, although I would
> have expected Array[Int | Long].
>
> On Wed, Nov 4, 2009 at 8:03 AM, Ricky Clarkson <ricky.clarkson@...>
> wrote:
>>
>> He meant "Array[Int] or Array[Long]".
>>
>> 2009/11/4 Nils Kilden-Pedersen <nilskp@...>:
>> > On Wed, Nov 4, 2009 at 7:29 AM, Viktor Klang <viktor.klang@...>
>> > wrote:
>> >>
>> >> either use @specialized
>> >
>> > I thought @specialized was for class definitions only?
>> >
>> >>
>> >> or Array[Int/Long] etc
>> >
>> > Is that valid syntax?
>> >
>>
>>
>>
>> --
>> Ricky Clarkson
>> Java and Scala Programmer, AD Holdings
>> +44 1565 770804
>> Skype: ricky_clarkson
>> Google Talk: ricky.clarkson@...
>> Google Wave: ricky.clarkson@...
>
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...
Google Wave: ricky.clarkson@...

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I tell you probably nothing new but that's somewhat possible right now
(aside from the ugly prefix notation in outputs)

On Wed, Nov 4, 2009 at 3:13 PM, Ricky Clarkson <ricky.clarkson@...> wrote:
> Array[Either[Int, Long]] works.  Though, yes, I'd prefer Either to be called |

scala> type |[A,B] = Either[A,B]
defined type alias $bar

scala> Array[Int | Long](Left(5),Right(12L))
res6: Array[|[Int,Long]] = Array(Left(5), Right(12))

and even

scala> implicit def makeLeft[A,B](a:A):Either[A,B] = Left(a)
makeLeft: [A,B](A)Either[A,B]

scala> implicit def makeRight[A,B](b:B):Either[A,B] = Right(b)
makeRight: [A,B](B)Either[A,B]

scala> Array[Int|String](5,"Wurst",123,2,6)
res8: Array[|[Int,String]] = Array(Left(5), Right(Wurst), Left(123),
Left(2), Left(6))

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Randall Schulz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday November 4 2009, Viktor Klang wrote:
> Sigh
>
> I'm tired of performance envy involving arrays.
> Arrays are a shallow abstraction over implementation detail
> (contigous memory).
> They are also horridly implemented in the JVM (and in Java).

I'd be happy to use any abstraction for integer-indexed collections
whose performance rivals that of arrays. Short of that, it's hardly
feasible to eschew arrays for production code.


Randall Schulz


Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I had an idea something like that was possible.  Does that scale up to
X | Y | Z, and work for pattern-matching?

2009/11/4 Johannes Rudolph <johannes.rudolph@...>:

> I tell you probably nothing new but that's somewhat possible right now
> (aside from the ugly prefix notation in outputs)
>
> On Wed, Nov 4, 2009 at 3:13 PM, Ricky Clarkson <ricky.clarkson@...> wrote:
>> Array[Either[Int, Long]] works.  Though, yes, I'd prefer Either to be called |
>
> scala> type |[A,B] = Either[A,B]
> defined type alias $bar
>
> scala> Array[Int | Long](Left(5),Right(12L))
> res6: Array[|[Int,Long]] = Array(Left(5), Right(12))
>
> and even
>
> scala> implicit def makeLeft[A,B](a:A):Either[A,B] = Left(a)
> makeLeft: [A,B](A)Either[A,B]
>
> scala> implicit def makeRight[A,B](b:B):Either[A,B] = Right(b)
> makeRight: [A,B](B)Either[A,B]
>
> scala> Array[Int|String](5,"Wurst",123,2,6)
> res8: Array[|[Int,String]] = Array(Left(5), Right(Wurst), Left(123),
> Left(2), Left(6))
>
> --
> Johannes
>
> -----------------------------------------------
> Johannes Rudolph
> http://virtual-void.net
>



--
Ricky Clarkson
Java and Scala Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...
Google Wave: ricky.clarkson@...

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Dave Griffith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Wow.  I can't tell if that's very, very right or very, very wrong.  Also not sure whether I would want those implicits in Predef, or far away from it.  Wow.

--Dave Griffith

Re: Arrays: Disappointed about ridiculous slow scala code compared to Java

by Johannes Rudolph-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Nov 4, 2009 at 3:24 PM, Randall R Schulz <rschulz@...> wrote:

> On Wednesday November 4 2009, Viktor Klang wrote:
>> Sigh
>>
>> I'm tired of performance envy involving arrays.
>> Arrays are a shallow abstraction over implementation detail
>> (contigous memory).
>> They are also horridly implemented in the JVM (and in Java).
>
> I'd be happy to use any abstraction for integer-indexed collections
> whose performance rivals that of arrays. Short of that, it's hardly
> feasible to eschew arrays for production code.

Shouldn't specialization solve most of the problems? (In Burkhard's
benchmark it wasn't faster because the expensive step was the boxing
and the boxing was necessary because Comparator was used which isn't
specialized.)

--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net
< Prev | 1 - 2 - 3 | Next >