Anonymous classes performances

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Anonymous classes performances

Steven Costiou-2

I have been playing a bit with anonymous subclasses, and instances of anon subclasses seem slower to execute code than "regular" subclasses instances, but sometimes they reach equivalent performances (for code defined in anon-subclasses). I don't understand why.

I have a class A with a method m.
B is subclass of A with a new method m1.
Anon-A is an anonymous class of A which also implements the m1 method.

I did various tests of speed and:

- when executing m, compiled in A, instances of Anon-A are around 10% slower than instances of B

- when executing m1, compiled in B and in Anon-A, performances are equivalent between instances of B and of anon-A

Is that a particular behavior of anonymous subclasses, like their instances can easily find behavior defined in their class but have to perform extra-work to find methods defined in their superclass (which is not anonymous) ? Is that vm related ? Other ?

Steven.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Anonymous classes performances

Denis Kudriashov
Hi Steven.

Could you show code which you measure?

2017-06-16 17:17 GMT+02:00 Steven Costiou <[hidden email]>:

I have been playing a bit with anonymous subclasses, and instances of anon subclasses seem slower to execute code than "regular" subclasses instances, but sometimes they reach equivalent performances (for code defined in anon-subclasses). I don't understand why.

I have a class A with a method m.
B is subclass of A with a new method m1.
Anon-A is an anonymous class of A which also implements the m1 method.

I did various tests of speed and:

- when executing m, compiled in A, instances of Anon-A are around 10% slower than instances of B

- when executing m1, compiled in B and in Anon-A, performances are equivalent between instances of B and of anon-A

Is that a particular behavior of anonymous subclasses, like their instances can easily find behavior defined in their class but have to perform extra-work to find methods defined in their superclass (which is not anonymous) ? Is that vm related ? Other ?

Steven.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Anonymous classes performances

Steven Costiou-2

You can find the code below. I just change the call to m by m1 to test the two methods.

I started again in a fresh pharo 6 image and now the results seem all similar for the following code. I will do all my tests again to see if it was my fault but it takes a lot of time (2 hours for each full test).

What sould be the expected results ? Should an instance of an anonymous class be as fast as a regular instance when calling the same code, being defined in the anon class or its super class ?

 

A implements m ^100 printString and B subclass of A implements m1 ^100 printString

a := B new.


class := A newAnonymousSubclass
        addSlot: (InstanceVariableSlot named: #y); compile: 'm1
    ^100 printString'; yourself.
c := A new becomeForward: class new.

res := Dictionary new.
col1 := OrderedCollection new.
col2 := OrderedCollection new.


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col1 add: [1000000 timesRepeat: [ a m ]] timeToRun].


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col2 add: [1000000 timesRepeat: [ c m ]] timeToRun].


res at: 'a' put: col1.
res at: 'c' put: col2.
res inspect

Steven.

 

Le 2017-06-16 17:31, Denis Kudriashov a écrit :

Hi Steven.
 
Could you show code which you measure?

2017-06-16 17:17 GMT+02:00 Steven Costiou <[hidden email]>:

I have been playing a bit with anonymous subclasses, and instances of anon subclasses seem slower to execute code than "regular" subclasses instances, but sometimes they reach equivalent performances (for code defined in anon-subclasses). I don't understand why.

I have a class A with a method m.
B is subclass of A with a new method m1.
Anon-A is an anonymous class of A which also implements the m1 method.

I did various tests of speed and:

- when executing m, compiled in A, instances of Anon-A are around 10% slower than instances of B

- when executing m1, compiled in B and in Anon-A, performances are equivalent between instances of B and of anon-A

Is that a particular behavior of anonymous subclasses, like their instances can easily find behavior defined in their class but have to perform extra-work to find methods defined in their superclass (which is not anonymous) ? Is that vm related ? Other ?

Steven.

 

 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Anonymous classes performances

Steven Costiou-2

I spent my day testing and comparing execution speed between classes and anonymous subclasses when i tried to compare two sets of values between pharo classes: they also differ.

In fact it seems that every instruction has a different execution speed if we run it enough times. So it seems impossible to precisely compare execution time between anon classes and pharo classes, at least i don't know how to do it. I computed confidence intervals of various measures, the differences in execution time that exist between classes and anonymous classes can be between -2% to + 9%. But comparing sets of speed between pharo classes, i also have similar intervals (but smaller, from -2% speed to +5% speed).

 

So maybe they have similar performances, at least sometimes they do and sometimes one is slightly faster than the other, but in the end it is not possible to tell. But maybe i'm looking for something which does not even exist, maybe anonymous classes are designed to be as fast as regular classes ? Or maybe it is common to have such variability when benchmarking code ?

 

Does it exist any instruction in pharo with a constant execution time ? (or it could be possible with one million years to compute enough speed tests and do an accurate mean...)

 

Steven.

 

Le 2017-06-16 18:14, Steven Costiou a écrit :

You can find the code below. I just change the call to m by m1 to test the two methods.

I started again in a fresh pharo 6 image and now the results seem all similar for the following code. I will do all my tests again to see if it was my fault but it takes a lot of time (2 hours for each full test).

What sould be the expected results ? Should an instance of an anonymous class be as fast as a regular instance when calling the same code, being defined in the anon class or its super class ?

 

A implements m ^100 printString and B subclass of A implements m1 ^100 printString

a := B new.


class := A newAnonymousSubclass
        addSlot: (InstanceVariableSlot named: #y); compile: 'm1
    ^100 printString'; yourself.
c := A new becomeForward: class new.

res := Dictionary new.
col1 := OrderedCollection new.
col2 := OrderedCollection new.


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col1 add: [1000000 timesRepeat: [ a m ]] timeToRun].


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col2 add: [1000000 timesRepeat: [ c m ]] timeToRun].


res at: 'a' put: col1.
res at: 'c' put: col2.
res inspect

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Anonymous classes performances

Ben Coman
@all,  Is there some way to disable garbage collection during such benchmarks?

cheers -ben


On Sun, Jun 18, 2017 at 3:51 AM, Steven Costiou <[hidden email]> wrote:

I spent my day testing and comparing execution speed between classes and anonymous subclasses when i tried to compare two sets of values between pharo classes: they also differ.

In fact it seems that every instruction has a different execution speed if we run it enough times. So it seems impossible to precisely compare execution time between anon classes and pharo classes, at least i don't know how to do it. I computed confidence intervals of various measures, the differences in execution time that exist between classes and anonymous classes can be between -2% to + 9%. But comparing sets of speed between pharo classes, i also have similar intervals (but smaller, from -2% speed to +5% speed).

 

So maybe they have similar performances, at least sometimes they do and sometimes one is slightly faster than the other, but in the end it is not possible to tell. But maybe i'm looking for something which does not even exist, maybe anonymous classes are designed to be as fast as regular classes ? Or maybe it is common to have such variability when benchmarking code ?

 

Does it exist any instruction in pharo with a constant execution time ? (or it could be possible with one million years to compute enough speed tests and do an accurate mean...)

 

Steven.

 

Le 2017-06-16 18:14, Steven Costiou a écrit :

You can find the code below. I just change the call to m by m1 to test the two methods.

I started again in a fresh pharo 6 image and now the results seem all similar for the following code. I will do all my tests again to see if it was my fault but it takes a lot of time (2 hours for each full test).

What sould be the expected results ? Should an instance of an anonymous class be as fast as a regular instance when calling the same code, being defined in the anon class or its super class ?

 

A implements m ^100 printString and B subclass of A implements m1 ^100 printString

a := B new.


class := A newAnonymousSubclass
        addSlot: (InstanceVariableSlot named: #y); compile: 'm1
    ^100 printString'; yourself.
c := A new becomeForward: class new.

res := Dictionary new.
col1 := OrderedCollection new.
col2 := OrderedCollection new.


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col1 add: [1000000 timesRepeat: [ a m ]] timeToRun].


10000 timesRepeat: [
    Smalltalk garbageCollect .
    col2 add: [1000000 timesRepeat: [ c m ]] timeToRun].


res at: 'a' put: col1.
res at: 'c' put: col2.
res inspect


Loading...