Ackermann's Function (Super slow with std::cout, Instant with out std::cout, why? )

Ok so today I got a weird one.
How come if I cout the values this can take forever to finish.
But when removing std::cout and only doing the math its done in a instant.?
Is STL really "this slow" or did I do something wrong.

BTW this is Ackermann's Function
Wiki: http://en.wikipedia.org/wiki/Ackermann_function

Here is the code:
(Refrence / Credits:
http://www.daniweb.com/software-development/cpp/threads/327087/ackermanns-function-in-c )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <Windows.h>

using namespace std;
int A(int, int);
int main()
{
	int m, n;
	cout<<"Enter a value for m: ";
	cin>>m;
	cout<<"Enter a value for n: ";
	cin>>n;

	A(m,n);

	system("PAUSE");

	return 0;
}
int A(int m,int n)
{
	if (m==0)
	{
		cout<<"A("<<m<<","<<n<<") ";
		return n+1;
	}
	else if (n==0)
	{
		cout<<"A("<<m<<","<<n<<") ";
		return A(m-1,1);
	}
	else
	{
		cout<<"A("<<m<<","<<n<<") ";
		return A(m-1, A(m,n-1));	
	}
}


If anyone can explain this to me it would be great.

Cheers
WetCode
Last edited on
Is STL really "this slow" or did I do something wrong.


It's not STL... it's just console output in general. And yes, it can be that slow.

Printing (and flushing) is slow.
Ok thanks for your reply.
Cheers
Topic archived. No new replies allowed.