Posts Tagged efficiency

Making a Point about Object Oriented Design

I’ve been doing OOD and OOP for what I consider to be a pretty long time. I learned C++ back in around ’92, which makes this 16 years later.

I haven’t written C++ every day for a number of years, but I did do it professionally for a 3-4 years and though I came late to the generic programming party I think I can say I’m pretty proficient.

Heck I even read the parts of the book about design principles.

So what? I know… I always have to ramble on… otherwise I would just post the graph, which would be even more pointless, so hold on a sec.

I picked up Java in, say, 1995 and I’ve been doing that professionally for the last few years. Code reviews, looked at a lot of Java code and so I felt like I knew good object oriented design.

Then I read this article where the dude, Stroustrup, basicly said: “hey! not every data structure is an class! some of them are just data structures! don’t make every collection of data a class”

And I thought “Holy cwap! I have been doing OOD like a nut!” I realized I had been brainwashed!

In Java everything is a class, but that doesn’t mean it has to be a class!

WTF does that mean?

Lemme provide an example. In the article, dude, talks about an “address” example which is fine, but not very computable and I likes the 3d graphics, so I decided to do a quantitative test to measure how much it would cost to have a struct-style class in Java and perform operations on it rather than have the operations be part of the class.

Since I am scatter brained, I also wanted to try some other stuff as well and see what the cost of various types of implementation would be.

Here is the stuff I wrote to test:

Impl. Claim to Fame
PointDirect write out normalize “method” inline
Point direct access to member variables
Pointy uses get and set exclusively
Pt + Pt_struct uses point data structure

For each implementation, I ran 100 million dot products and (2x) normalize invocations.

The code from the code that uses the struct looks like this

        long max = 100000000;
        Pt_struct a = new Pt_struct( 1, 2, 3 );
        Pt_struct b = new Pt_struct( 4, 5, 6 );
        start = System.nanoTime();
        for ( i = 0 ; i < max ; i++ ) {
            Pt.normalize( a );
            Pt.normalize( b );
            Pt.dot( a, b );
        }
        stop = System.nanoTime();

all the code is pretty similar… check out the links…

Here are my results:

Impl. Time in ns Graph
PointDirect 7276762448
Point 7260967079
Pointy 7124373741
Pt + Pt_struct 4461348165

Did you see that? Basicly none of the composition of function, getters/setters v/s direct access to member variable really made a rat’s fart worth of difference…

But the “struct class” based implementation is 38% faster than the average run time of the other implementations!

Can this be right? It seems insane! But it appears to be the case.

If it’s true, I have a lot of refactoring to do on ghia!

—-

(1-4461348165/((7276762448+7260967079+7124373741)/3))*100

Here is some stuff if you are interested. If you are not interested, it is still some stuff:

Advertisements

Leave a Comment