Vectors

Pages: 12
Is that the correct way to push struct objects to a vector btw?
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Stats {
    std::string type;
    std::vector<long_type> values;
    long_type nanosec=0, microsec=0, millisec=0;
    double sec=0;
};

std::vector<Stats> stats;
Stats Iteration;
Iteration.type = "Iteration";
Iteration.values.push_back(Iteration Fibonacci sequences);
Iteration.values.push_back(Time to execute);

stats.push_back(Iteration);


Will everything follow if I do it this way?

-----------------------------------------------

And if I return stats and use it in another function, how can I output this?
Can't use some sort of counter because of mixed data types?

Pffff
Last edited on
I realized how to output the value somewhat, but I only get one, not the entire calculation?

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
//------------------------------------------------------------------------------
// Name: fibonacciTimer
//------------------------------------------------------------------------------
std::vector<Stats> fibonacciTimer(size_t nthNumber)
{
    std::vector<Stats> stats;

        Stats Iteration;
        Stats Recursion;
        Iteration.type = "Iteration";
        Recursion.type = "Recursion";
        size_t IterationCount{};
        size_t RecursionCount{};

        auto timeStartIteration = std::chrono::high_resolution_clock::now();

            for (size_t i = 0, IterationCount = nthNumber; i <= nthNumber; i++, IterationCount--)
            {
                Iteration.values.push_back(fibonacciIteration(IterationCount));

                if (i % 5 == 0)
                {
                    cout << "\n " << Iteration.type << " " << std::setw(2) << IterationCount << "'th value is: ";
                    cout << std::setw(6) << Iteration.values[i];
                }
            }


        auto timeStopIteration = std::chrono::high_resolution_clock::now();
        long_type durationIteration = std::chrono::duration_cast<std::chrono::microseconds>(timeStopIteration - timeStartIteration).count();

        Iteration.microsec = durationIteration;
        Iteration.millisec = durationIteration/1000;
        Iteration.nanosec = durationIteration*1000;
        Iteration.sec = durationIteration/1000000; //Convert to double somehow

        stats.push_back(Iteration);

        cout << "\n";
        cout << "The duration of this task took: " << Iteration.microsec << " microseconds." << endl;
        cout << "The duration of this task took: " << Iteration.millisec << " milliseconds." << endl;
        cout << "The duration of this task took: " << Iteration.nanosec << " nanoseconds." << endl;
        cout << "The duration of this task took: " << Iteration.sec << " seconds." << endl; //Convert to double somehow?

for (size_t i = 0; i < stats.size(); i++) 
    {
        cout << stats[i].values[i] << " " << endl;
    }


When I run this, I only get the highest value but twice?:


=====================
===== MAIN MENU =====
1. Measure
2. Timer - Test only
2. Print - Test only
4. Exit
=====================
=====================

Make your choice:1
 Choose your number:16

 Iteration 16'th value is:    987
 Iteration 11'th value is:     89
 Iteration  6'th value is:      8
 Iteration  1'th value is:      1
The duration of this task took: 6523 microseconds.
The duration of this task took: 6 milliseconds.
The duration of this task took: 6523000 nanoseconds.
The duration of this task took: 0 seconds.
987
987
You only store one value in stats.

You print it once at line 47. I suspect you're printing it again somewhere else.

You still aren't computing the stats for the recursive method. To do that, I think you'll want to put most of the code inside a loop:
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
std::vector<Stats> fibonacciTimer(size_t nthNumber)
{
    std::vector<Stats> stats(2); // vector of 2 stats

    stats[0].type = "Iteration";
    stats[1].type = "Recursion";
    
    for (int approach = 0; approach<2; ++approach) {
	size_t count;
	long_type val;
        auto timeStart = std::chrono::high_resolution_clock::now();

	for (size_t i = 0, count = nthNumber; i <= nthNumber; i++, count--) {
	    switch (approach) {
	    case 0:
		val = fibonacciIteration(count);
		break;
	    case 1:
		val = fibonacciRecursion(count);
		break;
	    }
	    // You could replace the above switch statement with
	    // val = (approach ? fibonacciRecursion(count) : fibonacciIteration(count));
	    
	    stats[approach].values.push_back(val);

	    if (i % 5 == 0) {
		cout << "\n " << stats[approach].type << " " << std::setw(2) << count << "'th value is: ";
		cout << std::setw(6) << stats[approach].values[i];
            }
	}

	auto timeStop = std::chrono::high_resolution_clock::now();
	long_type duration = std::chrono::duration_cast<std::chrono::microseconds>(timeStop - timeStart).count();

        stats[approach].microsec = duration;
        stats[approach].millisec = duration/1000;
        stats[approach].nanosec = duration*1000;
        stats[approach].sec = duration/1000000; //Convert to double somehow

        cout << "\n";
        cout << "The duration of this task took: " << stats[approach].microsec << " microseconds." << endl;
        cout << "The duration of this task took: " << stats[approach].millisec << " milliseconds." << endl;
        cout << "The duration of this task took: " << stats[approach].nanosec << " nanoseconds." << endl;
        cout << "The duration of this task took: " << stats[approach].sec << " seconds." << endl; //Convert to double somehow?
    }

    
    for (size_t i = 0; i < stats.size(); i++) {
        cout << stats[i].values[i] << " " << endl;
    }
    return stats;
}



Topic archived. No new replies allowed.
Pages: 12