-1

Today I came into a problem:

I have to read data from a file, the file contains a lot of test cases, it looks like

N

N lines followed..

...

...

So I used while(scanf("%d", &n) && n!=-1), but it took me more than 5s to read all data. However, when I changed it into while(scanf("%d", &n) && n>-1), it just took me 800ms to read alll data. So I suppose that there is difference between speed of comparison operators in C++, and can anyone give me the order?

PS: my compiler is GCC 5.1.0

OK, let me show more details of this problem. The problem is here: http://acm.hdu.edu.cn/showproblem.php?pid=1171

Code with not equal is here:https://github.com/kimixuchen/codesnap/blob/master/greater

Code with gerater is here: https://github.com/kimixuchen/codesnap/blob/master/not_equal

kimixuchen
  • 155
  • 1
  • 9
  • 3
    Is it reading correctly in both cases? – Grantly Dec 30 '15 at 12:27
  • Take a look [here](https://stackoverflow.com/questions/1029992/is-the-inequality-operator-faster-than-the-equality-operator), [here](https://stackoverflow.com/questions/12135518/is-faster-than), and [here](https://stackoverflow.com/questions/7084741/less-than-vs-equal-to-for-efficiency-c-c). – Cory Kramer Dec 30 '15 at 12:30
  • just a wild guess, but while the first variant requires an actual numerical comparison, the latter can be implemented by a test on the most significant bit. – collapsar Dec 30 '15 at 12:31
  • 15
    You're reading a file **from disk**, and you think the slow part was a `!=` operator? – Rotem Dec 30 '15 at 12:31
  • On a second thought, are there test cases labelled as `-2, -3, ...`? Possibly some Of the labels are 'large' positive numbers which are greater than the maximum integer value `n`'s datatype and whose bit patterns translate into a negative integer (the compiler should throw at least a warning, i guess, but that depends on the compiler flags' settings) ? – collapsar Dec 30 '15 at 12:33
  • 1
    this just doesn't make sense, are you sure both loops loop the same number of times? – Selçuk Cihan Dec 30 '15 at 12:37
  • 4
    Maybe some weird disc caching? Does this happens reliably? No matter the order of tests? – Revolver_Ocelot Dec 30 '15 at 12:42
  • The two conditions are not the same. The first condition is true for more cases than the second condition, and therefore the `while` loop can iterate more times (depending on the input). – interjay Dec 30 '15 at 12:52
  • 2
    The idea that the performance difference was due to the performance of the comparison is absurd. The performance difference must be due to the semantic difference between the two comparisons or due to an error in the test process (including failing to take file caching into account). Early on it could hit a case in which the `!=` continued while the `>` stopped. – JSF Dec 30 '15 at 13:10
  • Show your entire test case, reduced to a dozen lines of code. – n. m. could be an AI Dec 30 '15 at 14:16
  • Only in the last test case, N is -1, just indicating that it's the end, before this, N is in range of [1, 50]. – kimixuchen Jan 01 '16 at 03:13

1 Answers1

-1

The question is about comparison, not reading files or badly formulated conditions. So lets test comparison only. Update: tested with /O2 optimization option.

#include <ctime>
#include <cstdlib>
#include <iostream>
int main()
{

    const int testCases = 10000000;
    const int iterations = 100;
    srand(time(NULL));
    int * A = new int[testCases];
    bool *B = new bool[testCases];
    freopen("output.txt", "w", stdout);

    for (int i = 0; i < testCases; i++)
    {
        A[i] = rand() % 100;
    }

    clock_t begin = clock();

    for (int j = 0; j < iterations; j++)
    for (int i = 0; i < testCases; i++)
    {
        B[i] = A[i] != -1;
    }
    clock_t end = clock();
    double elapsed_secs = end - begin;
    std::cout << "Elapsed time using != - " << elapsed_secs << std::endl;
    //Getting new random numbers for clean test
    
    for (int i = 0; i < testCases; i++)
    {
        A[i] = rand() % 100;
    }

    begin = clock();

    for (int j = 0; j < iterations; j++)
    for (int i = 0; i < testCases; i++)
    {
        B[i] = A[i] > -1;
    }
    end = clock();
    elapsed_secs = end - begin;
    std::cout << "Elapsed time using > - " << elapsed_secs << std::endl;
    return 0;
}

Results for 5 tests (in ticks):

'!=': 1005 994 1015 1009 1019

'>': 1006 1004 1004 1005 1035

Conclusion - there is no significant difference in optimized for speed program.

Community
  • 1
  • 1
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • You aren't seeding your `rand() ` :P – Hatted Rooster Dec 30 '15 at 13:13
  • 7
    Without optimization the comparison is pointless. With optimization, the thing you want to test has been optimized out. Performance testing tiny bits of code is not the trivial task you seem to assume. – JSF Dec 30 '15 at 13:14
  • @YSC size_t cant help here. "Total size of array must not exceed 0x7fffffff" exception is raised if number of test cases gets bigger. I am going to use series of test to test optimized code. – Pavel Oganesyan Dec 30 '15 at 15:09
  • Thank you for your code. I just tested `!=` and `>` with method like yours, and the result showed that their speed is quite similar, so I felt more confused. I just added the specific problem and my code, you can take a look for more details. – kimixuchen Jan 01 '16 at 04:25