[C++] Need help optimizing performace of a small function

Started by
12 comments, last by Roots 12 years, 5 months ago

So I think I worked out a much faster solution. I will write the pseudo code and use your example


If you read the matrix formulation I proposed, what your code is doing is its most naive implementation. And yes, this is a very reasonable solution.
Advertisement

So I think I worked out a much faster solution.


I came up with this as well. I wouldn't be surprised if this could be solved in essentially constant time with respect to the wall's height with some fancy math involving the ratio of incoming to outgoing connections.

[quote name='landagen' timestamp='1321906965' post='4886326']
So I think I worked out a much faster solution. I will write the pseudo code and use your example


If you read the matrix formulation I proposed, what your code is doing is its most naive implementation. And yes, this is a very reasonable solution.
[/quote]


I was formulating my post when you posted yours and had not read yours. I think both will work just fine. In the case of sparse line compatibilities I think mine will consume less memory and will be faster, but yours will be quicker in the event of dense line compatibilities.



[quote name='alvaro' timestamp='1321907485' post='4886332']
[quote name='landagen' timestamp='1321906965' post='4886326']
So I think I worked out a much faster solution. I will write the pseudo code and use your example


If you read the matrix formulation I proposed, what your code is doing is its most naive implementation. And yes, this is a very reasonable solution.
[/quote]


I was formulating my post when you posted yours and had not read yours.[/quote]
Sure, I wanted to point out to the OP that we were basically saying the same thing.

I think both will work just fine. In the case of sparse line compatibilities I think mine will consume less memory and will be faster, but yours will be quicker in the event of dense line compatibilities.
[/quote]


Well, I didn't propose a specific algorithm to compute the product of matrices I posted. Your pseudocode is a perfectly good way to do it. If the height is large, a dense matrix representation will work better. It might even be worth diagonalizing the matrix, so this method can be used.
Ah, one more comment for the OP: If your function is not going to modify the container that it gets passed, make it a const reference.
Thanks for all your help thus far everyone. landagen, I implemented the algorithm you described and yes, it is much much faster. Easily fast enough for my requirements. Also I am using 64-bit integers to compute this total as that is the maximum size I am expecting for any given input. I still am not quite sure about how the solution works though. This is just something that I need to think about and study on my own though. I think if I work the algorithm through an example data set I will understand it better. Thanks so much, I really appreciate the help on this! :)

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

This topic is closed to new replies.

Advertisement