Faster than recursion

S

Stephen Howe

Is there a faster and less memory eating way to do things than recursion?

Yes

Stephen Howe
 
O

Ohmu

Ivan said:
Typically yes, in C/C++ as in other procedural languages.
but it depends on the specific algorithm you want to derecursify.
Sometimes a recursive call can be replaced by a simple loop.
Sometimes you need some type of cache/buffer/queue, which
makes the implementation more complicated.

Maybe you'll want to post a specific example...


hth


Well.. the code I have is tooo messy... I just try to explain what it
does...
There are some squares for example 6x7 and the recursion tryes to find
every possible combination of "x" that can be there without being on
each other way(only one in one column and one row and one in
diagonals)... so the function calls itself where it finds a spot where
the "x" can be put, and so every time until the size of the squares
are reached... but for this 6x7 squares the function calls it self
about 300-600 times.. it eats lot of memory and when the squares are
bigger it takes huge amount of time and loads(in MB) of memory...

Hope you understood :)

Ohmu
 
O

Ohmu

Bruce said:
Sounds a lot like the 8 queens problem. Is that what it is? If so, just
say so instead of being evasive. Or are you wanting homework done for you?

eight queen problem?? what's that? no homework that kind...
 
E

E. Robert Tisdale

Ohmu said:
Is there a faster and less memory eating way to do things than recursion?

No.

A *good* optimizing C++ compiler will "flatten"
a recursive [inline] function call automatically
if it really can realize an advantage by doing so.
 
K

Karl Heinz Buchegger

Ohmu said:
Well.. the code I have is tooo messy... I just try to explain what it
does...
There are some squares for example 6x7 and the recursion tryes to find
every possible combination of "x" that can be there without being on
each other way(only one in one column and one row and one in
diagonals)... so the function calls itself where it finds a spot where
the "x" can be put, and so every time until the size of the squares
are reached... but for this 6x7 squares the function calls it self
about 300-600 times.. it eats lot of memory and when the squares are
bigger it takes huge amount of time and loads(in MB) of memory...

8 queens problem modified?

This should not take lots of Megabytes and time. Not with
your problem domain.

Sounds more like you have a bug in your function.
 
M

Michiel Salters

E. Robert Tisdale said:
Ohmu said:
Is there a faster and less memory eating way to do things than recursion?

No.

A *good* optimizing C++ compiler will "flatten"
a recursive [inline] function call automatically
if it really can realize an advantage by doing so.

Most compilers won't guess at the input parameters, and usually
the decision critically depends on the parameters. Even worse,
once parameters are passed by value, copy ctors may be needed that
can't be elided trivially.

This would be the case if the TS passed his array inside a class
and by value; copying that 42-element array probably stops many
optimizers.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top