Vector Arithmetic using Primitives

When we were talking about OpenGL we looked at other solutions and what you need is very simple: you need mostly 3-dimensional or 4-dimensional vectors – and these vectors are mainly given to the OpenAPI’s. Sometimes only one instance, but very often as vectors of OSFloat32 or OSFloat64.

These subclasses are well suited to be used as transfer instances for values to OpenGL – but they are worse, if you want to do heavy calculations with them.

These classes are also pretty well useable for vectors – and they give you the option to transfer values to os based memory.

But they are slow ….

Therefore people very often are using subclasses of Array to simulate vectors – and these solutions are pretty often the fastest available.

Therefore you have two possibilities:

* subclasses of OSStructure
* subclasses of Array

One of the worst problems of these classes are accessing: one-based index access when using Array and zero-based index access when using OSStructure solutions. That means, that code written for one solution is not suitable for the other one.

Regarding speed you may have a look at this picture shows benchmarks for vector addition:

benchmarks3

The upper lines show typical solutions of a vector addition using plain OSFloat32 classes in pure Smalltalk and typical code like:


array3 at: index put ((array1 at: index) + (array2 at: index))

The lines in the middle show solutions based on Arrays in pure Smalltalk. They show, that Array-based solutions are 3 times faster than the OSStructure-based solutions.

The lower lines show OSStructure solutions with a little help by primitives for float and doubles. These solutions are around 3 times faster than the Array based solutions and 10 times faster than the original OSStructure based solutions.

*Summary *

That means, that these primitive-supported solutions are the fastest solution and they are ideal suitable for transfering data to external C-API’s.

* Instance Creation is time consuming *

One additional word to the benchmarks – when building these benchmarks one can notice, that the creation of objects can be a pretty time consuming task. Therefore reusing of arrays might be a good idea to improve the speed of your application

This entry was posted in Smalltalk and tagged , , , . Bookmark the permalink.