#823 Speed question

DanielFath Wed 18 Nov 2009

How big is the speed difference between the two following lines?

Line 1:

b := Int[,]
[1,2,3,4].each |v,pos| {if (pos%3==0) b.add(v); }
b.clear
[1,2,3,4,5,6,7,8,9].each |v,pos| {if (pos%4==0) b.add(v); }

Line 2:

b := Int[,]
[1,2,3,4].each |v,pos| {b.add(v[pos]); }
b.clear
[[1,2,3],[4,5,6],[7,8,9]].each |v,pos| {b.add(v[pos]); }

I'm trying to make a simple Matrix so I'm kinda stuck whether to represent it as a single list which I iterate through or make it a list of lists.

PS. I assume mod (in general) is a more expensive operation but I'm unsure how would such cost scale.

Is there any sort of framework for speed testing programs?

BTW Fantom needs generics :-P

brian Wed 18 Nov 2009

I would expect flatten lists to perform better since it requires fewer objects to allocate, GC, index etc. Integer math operations like mod are typically super fast, especially if you are using unboxed ints.

Right now the best way to measure performance is using sys::Duration:

t1 := Duration.now
Actor.sleep(200ms)
t2 := Duration.now
echo("Time was ${(t2-t1).toLocale}")

Some sort of StopWatch utility might be nice eventually.

tactics Wed 18 Nov 2009

BTW Fantom needs generics :-P

Fantom's stance on generics is their usefulness doesn't outweigh their cost in complexity. The reflection API is super, super simple, and that's a good thing.

The lack of generics in Fantom seems to draw a bit of criticism from Java and C# programmers. But this is only because they are used to very heavy type systems. Fantom is is a dynamically typed wolf in a sheep's statically typed clothing.

See also: docIntro::WhyFantom Generics

Some sort of StopWatch utility might be nice eventually.

Indeed. I was playing around with a Timer class a while ago that made use of it blocks. It would look something like this:

doer := Doer()
t := Timer 
{
  doer.foo
  split
  doer.bar
  split
  doer.baz

}
echo(t.start)  // Duration when it block began to run
echo(t.stop)   // Duration when it block ended
echo(t.total)  // Duration of t.stop - t.start
echo(t.splits) // List of Durations when it.split was called...
               //   like split/lap times on a stopwatch

helium Wed 18 Nov 2009

edit: deleted

qualidafial Wed 18 Nov 2009

Indeed. I was playing around with a Timer class a while ago that made use of it blocks. It would look something like this:

Presumably the timer is running in a separate thread, wouldn't the example you gave violate the single-thread constraint on mutable objects?

tactics Wed 18 Nov 2009

Presumably the timer is running in a separate thread, wouldn't the example you gave violate the single-thread constraint on mutable objects?

I'm not sure what you mean, exactly. The timer is simply taking snapshots of the system timer with Duration.now. Thanks to heavy abuse of the it-block constructor syntax, I even managed to make Timer a const class.

alexlamsl Wed 18 Nov 2009

Is Timer.start taken before the class is initialised? There could be constructor code from super-class, for instance, which is run (and hence takes a bit of time) before reaching doer.foo.

In addition, how would this implementation fare with JVM's general issue of micro-benchmarking? I guess if I put a big enough loop inside the block and run it for long enough, it should get equally optimised out to give the throughput that I would like to measure. With the exception when introducing Timer.split calls, may be.

Login or Signup to reply.