Why is the execution time 0ms?

Pages: 123
> So I did the following:
> Right click on the project -> Properties -> C/C++ -> Build Variables -> LD_LIBRARY_PATH
> and included the path the in the environment variable.

I've no familiarity at all with the Eclipse IDE.

Try setting the LD_LIBRARY_PATH environment variable in the shell, and then starting the Eclipse IDE from the shell.

For example, in the C shell
setenv LD_LIBRARY_PATH $LD_LIBRARY_PATH:/usr/local/bin/boostCppLib/lib
(Adding this line to .cshrc in your home directory would be convenient).
Hi,

#include </usr/local/bin/boostCppLib/include/boost/timer/timer.hpp>

That seems a strange location to install boost. The bin directories are supposed to be for executable (binary) files. In my system boost is in /usr/include/boost, so I would have :

#include </boost/timer/timer.hpp>

You could just move the files there, IIRC that's what I did in my install.

And you seemed to have lumped all the libraries in there too. I installed boost into /opt/boost1_61 ; the libraries are in the libs directory under that.
Thank you for your answers.

I am finally able to use the boost library timer :) yuhu!!. However I am surprised to see that, considering the processor time, the iterative code executes slower than the recursive code (I was expecting the other way around). this is my code:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <boost/timer/timer.hpp>
using namespace std;

//Prototypes
double calculoFactorialRecursividad(double, const double);

//Global variables
int loop_it=10000000;


int main() {
	double fac,fac1, tmp1, tmp2;
	double duration;

	fac=100;
	fac1=fac;

	tmp1 = fac-1;
	tmp2 = tmp1;

    boost::timer::auto_cpu_timer auto_timer( 3, " boost_it:  processor %t seconds\n"
                                                 " boost_it: wall clock %w seconds\n"
                                                 " boost_it:  CPU usage %p%\n\n" ) ;
	for (int i=0; i<loop_it; i++){
		while (tmp1>0){
			fac*=tmp1;
			tmp1--;
		}
	}

    boost::timer::auto_cpu_timer auto_timer1( 3, " boost_rec:  processor %t seconds\n"
                                                 " boost_rec: wall clock %w seconds\n"
                                                 " boost_rec:  CPU usage %p%\n\n" ) ;

	for (int i=0; i<loop_it; i++){
		while (tmp2>0){
			fac1 = calculoFactorialRecursividad(fac1,tmp2);
			tmp2--;
		}
	}

	return 0;
}


double calculoFactorialRecursividad(double fac1, const double tmp2){
	fac1*=tmp2;
	return fac1;
}


This is the output:
boost_rec: processor 0.050 seconds
boost_rec: wall clock 0.103 seconds
boost_rec: CPU usage 48.4%

boost_it: processor 0.100 seconds
boost_it: wall clock 0.248 seconds
boost_it: CPU usage 40.3%


What is wrong? Iterative code should be always executed faster than recursive code
> Iterative code should be always executed faster than recursive code

Not necessarily. With modern compilers (particularly clang++), often simple tail-recursive code executes a wee bit faster than iterative code. However, when the recursive code is slower (for instance, if non-trivial destruction of local objects are involved), it is very much slower.

Incidentally, there is no recursive code in your program, the second version too is iterative; the difference being that a function is called during each iteration.

The auto_cpu_timer prints timer statistics when the object is destroyed; so the first timer is printing the time taken from the time it was constructed till the end of main. (Which is why the output with the tag boost_it appears after the one with the tag boost_rec.)

Both these pieces of code (the two versions computing the approximate factorial) should take roughly the same amount of time.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <boost/timer/timer.hpp>
using namespace std;

//Prototypes
double calculoFactorialRecursividad(double, const double);

//Global variables
constexpr long long loop_it = 2'000'000'000 ;


int main() {
	double fac,fac1, tmp1, tmp2;
	// double duration;

	fac=100;
	fac1=fac;

	tmp1 = fac-1;
	tmp2 = tmp1;
    
    {
        
        boost::timer::auto_cpu_timer auto_timer( 3, " boost_it:  processor %t seconds\n"
                                                     " boost_it: wall clock %w seconds\n"
                                                     " boost_it:  CPU usage %p%\n\n" ) ;
        for (long long i=0; i<loop_it; i++){
            while (tmp1>0){
                fac*=tmp1;
                tmp1--;
            }
        }
        
        // destructor of auto_timer will now print out the elapsed times for the iterative version
    }

    boost::timer::auto_cpu_timer auto_timer1( 3, " boost_rec:  processor %t seconds\n"
                                                 " boost_rec: wall clock %w seconds\n"
                                                 " boost_rec:  CPU usage %p%\n\n" ) ;

	for (long long i=0; i<loop_it; i++){
		while (tmp2>0){
			fac1 = calculoFactorialRecursividad(fac1,tmp2);
			tmp2--;
		}
	}

	return 0;
}


double calculoFactorialRecursividad(double fac1, const double tmp2){
	fac1*=tmp2;
	return fac1;
} 

 boost_it:  processor 2.750 seconds
 boost_it: wall clock 2.752 seconds
 boost_it:  CPU usage 99.9%

 boost_rec:  processor 2.730 seconds
 boost_rec: wall clock 2.732 seconds
 boost_rec:  CPU usage 99.9%

http://coliru.stacked-crooked.com/a/128ead3255a90361
JLBorges Thanks for your answer.

the second version too is iterative; the difference being that a function is called during each iteration


How could I implement recursive code for the second version?
My main target is to demonstrate that recursive code normally executes slower than iterative code.
I thought that putting a function would make the code more recursive :S

Last edited on
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
38
39
40
41
42
43
#include <iostream>
#include <boost/timer/timer.hpp>

unsigned long long factorial_iterative( unsigned int n ) {

    unsigned long long factorial = 1 ;
    for( unsigned int i = 2 ; i <= n ; ++i ) factorial *= n ;
    return factorial ;
}

unsigned long long factorial_recursive( unsigned int n ) {

    return n < 2 ? 1 : n * factorial_recursive(n-1) ;
}

//Global variables


int main() {

    constexpr long long N = 1'000'000'000 ;

    volatile long long result = 0 ;

    {

        boost::timer::auto_cpu_timer auto_timer( 3, " boost_it:  processor %t seconds\n"
                                                    " boost_it: wall clock %w seconds\n"
                                                    " boost_it:  CPU usage %p%\n\n" ) ;

        for( long long i = 0 ; i<N; ++i ) result = factorial_iterative(20) ;
    }

    {
        boost::timer::auto_cpu_timer auto_timer( 3, "boost_rec:  processor %t seconds\n"
                                                    "boost_rec: wall clock %w seconds\n"
                                                    "boost_rec:  CPU usage %p%\n\n" ) ;

        for( long long i = 0 ; i<N; ++i ) result = factorial_recursive(20) ;
	}
    
    std::cout << "\n\n" << result << '\n' ;
} 

http://coliru.stacked-crooked.com/a/574fd94cfa153f96
An example where recursion is significantly slower than iteration:

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
38
39
40
41
42
43
44
45
#include <iostream>
#include <boost/timer/timer.hpp>
#include <boost/multiprecision/cpp_int.hpp>

// http://www.boost.org/doc/libs/1_65_1/libs/multiprecision/doc/html/index.html
using big_int = boost::multiprecision::cpp_int ;

big_int factorial_iterative( unsigned int n ) {

    big_int factorial = 1 ;
    for( unsigned int i = 2 ; i <= n ; ++i ) factorial *= n ;
    return factorial ;
}

big_int factorial_recursive( unsigned int n ) {

    return n < 2 ? big_int(1) : n * factorial_recursive(n-1) ;
}

int main() {

    constexpr long long N = 1'000'000 ;
    constexpr unsigned int num = 100 ;

    big_int result = 0 ;

    {

        boost::timer::auto_cpu_timer auto_timer( 3, " boost_it:  processor %t seconds\n"
                                                    " boost_it: wall clock %w seconds\n"
                                                    " boost_it:  CPU usage %p%\n\n" ) ;

        for( long long i = 0 ; i<N; ++i ) result = factorial_iterative(num) ;
    }

    {
        boost::timer::auto_cpu_timer auto_timer( 3, "boost_rec:  processor %t seconds\n"
                                                    "boost_rec: wall clock %w seconds\n"
                                                    "boost_rec:  CPU usage %p%\n\n" ) ;

        for( long long i = 0 ; i<N; ++i ) result = factorial_recursive(num) ;
    }

    std::cout << "\n\n" << num << "! == " << result << '\n' ;
}

http://coliru.stacked-crooked.com/a/110b8f6a6b5b9e20
Topic archived. No new replies allowed.
Pages: 123