Robert Klemme
4/3/2007 9:42:00 AM
On 03.04.2007 08:07, bwv549 wrote:
> *** SUMMARY *** (for situations with gazillions of set-length
> objects):
> 1. don't use objects--if you can, use arrays. Faster and way leaner.
> 2. If you have to use an object, struct seems like a good
> compromise. Simple to use, fast, extendable, fast access by keywords,
> a little slow on object creation. Significantly more memory than an
> array, but much less than a normal object.
> 3. The ugly MyObject from above is probably best of you want faster
> initialization and plan to access mostly by index. However, it's not
> implemented in any easy to create way. This is where someone with some
> meta-programming magic might be able to really clean things up...
> 4. Avoid normal objects. They take a long time to initialize and
> take up a lot of memory.
I am not sure I agree to your assessments about performance. A class is
actually the fastest if you omit the initialize definition.
Also, I am not sure whether your memory measurements are accurate: as
far as I can see you do neither invoke GC.start (in order to try to
force GC, which is an unreliable method anyway) nor GC.disable (in order
to stop GC completely and thus be able to measure allocation).
Kind regards
robert
09:58:18 [Temp]: ./create.rb
Rehearsal --------------------------------------------------
Struct 3.281000 0.000000 3.281000 ( 3.360000)
Class 0.703000 0.000000 0.703000 ( 0.840000)
Class (init) 6.375000 0.000000 6.375000 ( 7.962000)
Class (init2) 7.203000 0.000000 7.203000 ( 8.807000)
Array 1.016000 0.000000 1.016000 ( 1.351000)
Array.new 0.812000 0.000000 0.812000 ( 0.881000)
Array.new(5) 1.516000 0.000000 1.516000 ( 1.890000)
OpenStruct 4.672000 0.000000 4.672000 ( 5.969000)
---------------------------------------- total: 25.578000sec
user system total real
Struct 2.687000 0.000000 2.687000 ( 3.359000)
Class 0.797000 0.000000 0.797000 ( 0.845000)
Class (init) 6.687000 0.000000 6.687000 ( 7.929000)
Class (init2) 7.016000 0.000000 7.016000 ( 8.815000)
Array 1.125000 0.000000 1.125000 ( 1.347000)
Array.new 0.781000 0.000000 0.781000 ( 0.875000)
Array.new(5) 1.547000 0.000000 1.547000 ( 1.879000)
OpenStruct 4.750000 0.000000 4.750000 ( 5.985000)
09:59:31 [Temp]: cat create.rb
#!/usr/bin/ruby
require 'ostruct'
require 'benchmark'
include Benchmark
St1 = Struct.new :f1, :f2, :f3, :f4, :f5
class St2; attr_accessor :f1, :f2, :f3, :f4, :f5; end
class St3
attr_accessor :f1, :f2, :f3, :f4, :f5
def initialize(f1=nil, f2=nil, f3=nil, f4=nil, f5=nil)
@f1=f1
@f2=f2
@f3=f3
@f4=f4
@f5=f5
end
end
class St4
attr_accessor :f1, :f2, :f3, :f4, :f5
def initialize(*a)
@f1, @f2, @f3, @f4, @f5 = a
end
end
REP = 1_000_000
bmbm(15) do |re|
re.report("Struct") { REP.times { St1.new } }
re.report("Class") { REP.times { St2.new } }
re.report("Class (init)") { REP.times { St3.new } }
re.report("Class (init2)") { REP.times { St4.new } }
re.report("Array") { REP.times { [] } }
re.report("Array.new") { REP.times { Array.new } }
re.report("Array.new(5)") { REP.times { Array.new(5) } }
re.report("OpenStruct") { REP.times { OpenStruct.new } }
end
09:59:33 [Temp]: