BTW, to better compare the two, unroll the inner loop ten times, put Object in a temp to eliminate the indirection, and use explicit temps, i.e.
| o object | o := Object new. object := Object. 3 timesRepeat: [ Smalltalk garbageCollect ]. result1 := [ object basicNew. object basicNew. object basicNew. object basicNew. object basicNew. object basicNew. object basicNew. object basicNew. object basicNew. object basicNew ] benchFor: 10 seconds. 3 timesRepeat: [ Smalltalk garbageCollect ]. result2 := [ o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy. o shallowCopy ] benchFor: 10 seconds. {result1. result2}.
_,,,^..^,,,_ (phone)
On Jul 29, 2016, at 2:02 AM, Denis Kudriashov dionisiydk@gmail.com wrote:
Hi.
I compared performance between object instantiation and object cloning. I was wondering that instantiation almost twice faster than clone (primitive 70 vs 148).
Could you explain why it like that and could it be improved?
I was think that new object construction is much complex because it requires to fill all object fields (header structure and etc). And I was think that copy is just simple function like memcpy which just copy bytes without any logic.
Here is my code:
object := Object new. 3 timesRepeat: [ Smalltalk garbageCollect ]. result1 := [ Object basicNew ] benchFor: 10 seconds. 3 timesRepeat: [ Smalltalk garbageCollect ]. result2 := [ object shallowCopy ] benchFor: 10 seconds. {result1. result2}. "an Array(a BenchmarkResult(518,021,045 iterations in 10 seconds 2 milliseconds. 51,791,746 per second) a BenchmarkResult(302,807,253 iterations in 10 seconds 4 milliseconds. 30,268,618 per second))"
(I run it on latest Pharo on Mac SpurVM)
Best regards, Denis