Help with reused equations

So I'm making a program where I use sorts. In my output I'm trying to print the time it took and the relative # of copies and compares. I'm doing a switch in my main. The problem is I don't want to type the equation every time I use it for the time and #. So I put it before the switch, the problem is that it only outputs 0 for the time and the #'s

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
     for (int i = 0; i < 5; i++)
    {
        readem(data);
        numcopies = 0;
        numcomp = 0;
        auto start = high_resolution_clock::now();
        auto stop = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>(stop-start);  
        durationTime = duration.count();
        relComp = (double)numcomp / maxn;
        relCopies = (double)numcopies / maxn;

        switch (i)
        {

            case 0:
                name = "Bubble Sort:";
                start;
                bubbleSort(data);
                stop;
                duration;
                relComp;
                relCopies;
                printem(outf, data, name);
                break;
Last edited on
Well, you
- started the clock on line 6 ...
- and stopped the clock on line 7 ...
- and computers are pretty quick!
so is it really surprising that it records time zero?!

You have already computed all the timing variables etc BEFORE you did any sorts. Lines 18, 19, etc do nothing. They do NOT re-use equations.
Last edited on
Yea I thought about it being fast that's why I did microseconds. As far as the relative #'s, it works when I put the whole equation each time in each case but it doesn't when I put it before.
You were right, I have a pretty fast computer so the value was outputting 0 in microseconds. I changed it to nanoseconds and got an output.

Any idea on why it's outputting 0 for the relative #'s

it works when I put the whole equation each time in each case but it doesn't when I put it before.
That's because start = high_resolution_clock::now(); is not an equation*, it's an assignment. Just writing start; does absolutely nothing. The compiler generates no code for that line.
If you want to change the value of a variable you have to assign to it.


* If it was an equation, you should be able to write
 
high_resolution_clock::now() = auto start;
and it should mean exactly the same. Yet, that's not valid C++.
Okay, I understand. Does that also hold true for my relComp and relCopies assignments?
I don't think you do understand, or you wouldn't be asking that. The same does apply to relComp and relCopies. Put them alone on a line and they will do ... nothing.
Yea I tested them and saw that. Not sure where it was getting it's output from
simulationspecimen wrote:
Not sure where it was getting it's output from


It will get "output" (if, by that, you mean the values of duration etc.) from when you computed them HERE:
1
2
3
4
5
6
        auto start = high_resolution_clock::now();
        auto stop = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>(stop-start);  
        durationTime = duration.count();
        relComp = (double)numcomp / maxn;
        relCopies = (double)numcopies / maxn;

It's just that that is NOT near the sorting routines.
Last edited on
I think you want something more like this:
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
for (int i = 0; i < 5; i++) {
    readem(data);
    numcopies = 0;
    numcomp = 0;
    auto start = high_resolution_clock::now();

    switch (i) {

    case 0:
	name = "Bubble Sort:";
	bubbleSort(data);
	break;
    case 1:  ...
    case 2:  ...
    case 3:  ...
    case 4:  ...
    }

    auto stop = high_resolution_clock::now();
    auto duration = duration_cast < microseconds > (stop - start);
    durationTime = duration.count();
    relComp = (double) numcomp / maxn;
    relCopies = (double) numcopies / maxn;

    printem(outf, data, name);

Topic archived. No new replies allowed.