CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: more sorting

  1. #1
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    more sorting

    Attached is a zip archive containing a few test
    cases for sorting. Included in the archive is
    a file : sorting.txt , which contains a brief
    description of the test cases and some timing
    results.

    Comments and criticisms are welcome.

    I can provide a ".tar.gz" file if anyone prefers.
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    As for your errors:

    Depending on how the implementation of virtual base classes is done, this can lead to failures when these objects are not copied properly (which is what qsort / QSort does). Possibly GNU and PG use the same or similar technology in implementing these types of classes. Regarless of the reason for failure, everyone stated that the behavior of copying non-POD objects without invoking a proper copy constructor is undefined behavior. std::sort stood out without any problems, no matter which compiler it was run on. The emphasis should be why std::sort works and q/QSort doesn't (again the virtual bae classes looks like the deciding factor). If qsort didn't work and the reason was the kernel was messing up the code somehow, then other non-sort related, sound valid ANSI specification code would also fail that does a few memcpys().

    I would change the class type to non-virtual base classes and re-run. If you don't get the errors, then this shows that virtual base classes in PG and g++ cannot be moved around without invoking the correct copy constructors.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340

    It works just fine.

    Philips:

    I downloaded your files, and compiled the error.cpp, which is supposed to give an error, on VC++, it works just fine, without a single line of modification.

    I am posting the source code, the compiler output and the executable all in one package to show it indeed works on VC++.

    If you claim it doesn't work on your compiler, you have to put your compiler output to prove it, not just the soucre file. The source file works just fine on my compilers.

  4. #4
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340

    Here is my test files zipped

    Oops, forgot the attachment in last post.
    Attached Files Attached Files

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Anthony,

    If you had read the text in sorting.txt, you
    would have noticed I said it did not work
    under Red Hat Linux 7.1 (both g++ and
    pgCC)

  6. #6
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    Philips:

    If you read your own code again, you will realize that you did not actually randomize the array (for the double) before sorting. The array is actually already in order, So when doing std::sort it really doesn't do any thing. Meanwhile I identifies there is a bug in the Qsort code that it would swap two elements un-necessarily some times even when it is not necessary.

    If you REALLY randomize the array, you will see that even for double, even for this buggy version of Qsort, std::sort really is not much faster than Qsort.

    I will study and fix the bug in Qsort, then you are going to see Qsort beats std::sort even for doubles.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Concerning the double - they are not in order.
    They increase , then decrease. And as I mentioned
    in sorting.txt, for most data sets of double std::sort() is
    from 1.2 to 1.9 times faster than Qsort().
    Last edited by Philip Nicoletti; November 27th, 2002 at 11:56 AM.

  8. #8
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    then you are going to see Qsort beats std::sort even for doubles.
    When operating under less restrictions, such as relying on compiler-dependent behavior and ignoring the standard, I'd expect to be able to make anything faster.

    Performance doesn't mean anything if the answer is not correct, and your solution only works on a subset of compilers. It is simply not guaranteed to work on any compiler, any version of the compiler, or any operating system.

    Jeff

  9. #9
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by AnthonyMai
    I will study and fix the bug in Qsort, then you are going to see Qsort beats std::sort even for doubles.
    std::sort is twice as fast as Qsort on a vector of doubles I tried. And about 30 times faster than qsort.

    My results in milliseconds for 1 million doubles:
    • std::sort 237.8917
    • qsort 17659.1570
    • Qsort 512.8487


    P.S. My iostreams are broken and I use a windows-specific timer (written by alexey). Sorry for that
    Attached Files Attached Files
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured