CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 65
  1. #46
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: CArray vs. std::vector

    Quote Originally Posted by JohnW@Wessex View Post
    When you've only got a few minutes between compilations to write an example then you're not going to get anything ground-breaking
    Right.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  2. #47
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CArray vs. std::vector

    Quote Originally Posted by Philip Nicoletti View Post
    4) Have you posted code for sorting CLIst yet ?
    The hackish way to do this with MFC is to copy the CList to a temp CArray, sort the CArray using std::sort, and then rebuild the CList from the sorted CArray.

    That was the usual solution I have given here in the past. Produces the least bugs, and efficiency isn't too bad.

    Regards,

    Paul McKenzie

  3. #48
    Join Date
    Jun 2008
    Posts
    592

    Re: CArray vs. std::vector

    Code:
    qsort(arr.GetData(), size, sizeof(CString*), CompareStrings);
    Quote Originally Posted by Philip Nicoletti
    4) using qsort on a container of CString is technically undefined,
    but since MS does it, I'll let it slide. But using qsort on other
    non-POD elements still should not be done....?
    If you used undefined behaviour for cStringArray, I should be able to use undefined behaviour for vector. Vector would win in this case.

    letting undefined behaviour slide like that will never be portable. microsoft's code sometimes is not so great at times, but I would never use such disregard by using non-pod as pod. This will lead to even more undefined behaviour... like trying to call a variadic function with non-pod arguments and expect defined results.

    if you want to time something fairly, I will be more than happy to compare results

    Here is the stl_sort fixed to improve speed

    Code:
    // stl_sort.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include "array.h"
    bool Comp( const CString& first, const CString& second)
    {
        return first.Compare( second );
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
        ShowWindow(::GetConsoleWindow(), SW_HIDE);
        const size_t size = GetArraySize();
        LARGE_INTEGER begin, end, freq;
        ::QueryPerformanceFrequency(&freq);
        ::QueryPerformanceCounter(&begin);
        std::vector<CString> v;
        v.assign( &g_strings[0],  &g_strings[ size ] );
        ::QueryPerformanceCounter(&end);
        double fill = ((double)end.QuadPart - begin.QuadPart) * 1000.0 / freq.QuadPart;
        ::QueryPerformanceCounter(&begin);
        std::sort(v.begin(), v.end(), Comp);
        ::QueryPerformanceCounter(&end);
        double sort = ((double)end.QuadPart - begin.QuadPart) * 1000.0 / freq.QuadPart;
        CString strResult;
        strResult.Format(_T("Array size: %u")
                         _T("\nFill time: %lf ms.")
                         _T("\nSort time: %lf ms.\n"),
                         size, fill, sort);
        ::MessageBox(NULL, strResult, _T("STL WAY"), MB_OK);
        return 0;
    }
    MFC way
    Array size: 29979
    Fill time: 7.638157 ms
    Sort time: 10.190586 ms.

    Stl way
    Array size: 29979
    Fill time: 6.434275 ms
    Sort time: 15.723842 ms.

    mfc way total = 17.828743
    stl way total = 22.158117

    stl is a tad bit slower, but portability and standard comes to mind. I am all for speed, but not when it breaks standard rules. I hate tracking bugs down and creating them on purpose to begin with isn't the way I want to start off creating a project.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  4. #49
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: CArray vs. std::vector

    You're passing a function pointer into sort(), there. While it's possible the compiler might be able to inline that, in general it's easier to inline functors. I'd be curious to know if it does any better if you define it as:

    Code:
    struct Comp
    {
        bool operator()( const CString& first, const CString& second) const
        {
            return first.Compare( second );
        }
    };
    ....
    std::sort(v.begin(), v.end(), Comp());

  5. #50
    Join Date
    Jun 2008
    Posts
    592

    Re: CArray vs. std::vector

    The result was the same in this case.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  6. #51
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: CArray vs. std::vector

    You could also have combined construction and assignment, though I doubt that would have made any noticeable difference.

    Code:
    std::vector<CString> v( &g_strings[0],  &g_strings[ size ] );
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #52
    Join Date
    May 2001
    Posts
    8

    Re: CArray vs. std::vector

    It's amazing how so many people keep failing to answer the question.

    The original poster asked which containers were faster, and which were better. He didn't ask for people to go off on a ideological trip about standards.

    I'll only address the speed question. In my tests, STL containers are not always particularly fast. In many cases, the MFC containers are faster, sometimes dramatically so. If speed is your concern (and it's sometimes an absolutely fundamental issue), I would tend to favour the MFC containers, but it's better to evaluate on a case-by-case basis. My advice: do a comparative test with both with the kind of data your app will use.

    A hopelessly slow application in 100% standard C++ is no use to anybody.

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

    Re: CArray vs. std::vector

    Can you post your tests ?? It has been shown on Codeguru and other locations
    the std::vector is as fast or faster than CArray. (I hope you were not testing
    Debug version or with iterator checking).

  9. #54
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CArray vs. std::vector

    Quote Originally Posted by LordLiverpool View Post
    It's amazing how so many people keep failing to answer the question.

    The original poster asked which containers were faster, and which were better. He didn't ask for people to go off on a ideological trip about standards.
    Isn't discussion of "better" include discussion of standards?
    I'll only address the speed question. In my tests, STL containers are not always particularly fast. In many cases, the MFC containers are faster, sometimes dramatically so.
    Let's see your tests against optimized code, with the proper preprocessor flags used in creating the program (_SECURE_SCL=0 is the one that people often missed if you use Visual Studio).
    A hopelessly slow application in 100% standard C++ is no use to anybody.
    Show your tests. Compiler, version, whether it is optimized properly, etc.

    Regards,

    Paul McKenzie

  10. #55
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: CArray vs. std::vector

    Quote Originally Posted by Paul McKenzie View Post
    Isn't discussion of "better" include discussion of standards?
    Let's see your tests against optimized code, with the proper preprocessor flags used in creating the program (_SECURE_SCL=0 is the one that people often missed if you use Visual Studio). Show your tests. Compiler, version, whether it is optimized properly, etc.

    Regards,

    Paul McKenzie
    Paul, have you ever tried the "sorting project" posted here with and without _SECURE_SCL=0 or we are just talking using "holy scripts"?

    Regards,
    Ovidiu
    Last edited by ovidiucucu; March 4th, 2011 at 11:25 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #56
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CArray vs. std::vector

    Quote Originally Posted by ovidiucucu View Post
    Paul, have you ever tried the "sorting project" posted here with and without _SECURE_SCL=0 or we are just talking using "holy scripts"?

    Regards,
    Ovidiu
    The _SECURE_SCL=0 turns off iterator checking in release mode. With iterator checking on, the code will show degradation. It seems your original project did not have this set in release mode. Rerun your tests, but set this flag.

    The issue is that Visual Studio doesn't have this set to 0 when building a release version, thereby the STL applications were slower than they should be if you were not aware of this flag. This has been reported to MS as to the flaw, and some haven't had too many nice words to say to the VS engineers about it.

    Secondly, I think I mentioned that STL is not just containers -- you have the algorithm functions. MFC doesn't have these functions, so the advice "never to use STL in an MFC application" needs to be clarified.

    Regards,

    Paul McKenzie

  12. #57
    Join Date
    Jan 2001
    Posts
    253

    Re: CArray vs. std::vector

    Quote Originally Posted by Paul McKenzie View Post
    The issue is that Visual Studio doesn't have this set to 0 when building a release version, thereby the STL applications were slower than they should be if you were not aware of this flag. This has been reported to MS as to the flaw, and some haven't had too many nice words to say to the VS engineers about it.
    This has been fixed in Visual Studio 2010 - the default for debug mode is still to have checked iterators, but release mode has _SECURE_SCL set to 0 (at least by default).

    Best regards,
    John

  13. #58
    Join Date
    Apr 1999
    Posts
    27,449

    Re: CArray vs. std::vector

    Quote Originally Posted by jwbarton View Post
    This has been fixed in Visual Studio 2010 - the default for debug mode is still to have checked iterators, but release mode has _SECURE_SCL set to 0 (at least by default).

    Best regards,
    John
    OK, good to know.

    Regards,

    Paul McKenzie

  14. #59
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: CArray vs. std::vector

    Quote Originally Posted by Paul McKenzie View Post
    The _SECURE_SCL=0 turns off iterator checking in release mode. With iterator checking on, the code will show degradation. It seems your original project did not have this set in release mode. Rerun your tests, but set this flag.

    The issue is that Visual Studio doesn't have this set to 0 when building a release version, thereby the STL applications were slower than they should be if you were not aware of this flag. This has been reported to MS as to the flaw, and some haven't had too many nice words to say to the VS engineers about it.
    I knew that.
    Ok, seems to be logical and good to add "no additional run-time checking, means more speed" in Programmer's Holy Book.
    However, until seeing somethig is really real, I'm Thomas The Unbeliever Programmer so I said: "Let's try it!".
    I was expecting a significant speed increasing by disabling run-time iterators checking (setting _SECURE_SCL=0), but... surprise: the result was quite the same.
    Or course, would be stupid to generalize and take that as a generally true statement.
    In other non-trivial cases, the result may be as "canonically" expected.
    Maybe I was wrong, then you can try it. That program is not tabu and doesn't bite. .


    Quote Originally Posted by Paul McKenzie View Post
    Secondly, I think I mentioned that STL is not just containers -- you have the algorithm functions. MFC doesn't have these functions, so the advice "never to use STL in an MFC application" needs to be clarified.
    I said:
    Quote Originally Posted by me View Post
    DO NOT use STL in an MFC project if it's not absolutelly necessary.
    Is it absolutelly necessary to show "if it's not absolutelly necessary" phrase by using big bright letters and having Las Vegas effects?
    Well, I developed MFC applications for years and, generally, there were rare cases of "absolutelly necessary".

    One beside note.
    Run-time ckecking is not evil. In many cases, a little bit smaller speed performance is preferable to other nasty problems.
    As an example, once I found something like this:
    Code:
          v.erase(iter);
          //...
          --iter;
    This is just a trivial example. May be others, having more insidious sources of painful run-time headaches.
    We believe or not, this is the real mortals' world, below the clouds.
    Last edited by ovidiucucu; March 5th, 2011 at 03:45 AM. Reason: typos
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  15. #60
    Join Date
    May 2009
    Posts
    2,413

    Re: CArray vs. std::vector

    Quote Originally Posted by LordLiverpool View Post
    A hopelessly slow application in 100&#37; standard C++ is no use to anybody.
    That's right. A lightning fast application in 100% standard C++ is much better.

    There's nothing inherently slow with the C++ standard libraries. And from what I know the Microsoft implementation is licenced from Dinkumware, a high quality provider. They wouldn't get away with a crappy implementation. And if Microsoft implemented it themselves I don't think they would have anything to gain from making their C++ standard libraries inferior on purpose to promote the MFC?

    In my tests, STL containers are not always particularly fast. In many cases, the MFC containers are faster, sometimes dramatically so.
    I've heard claims like this over and over again in forums and each time when the benchmarks are finally posted the only thing they prove conclusively is that the poster doesn't know what he's doing.
    Last edited by nuzzle; March 5th, 2011 at 03:23 AM.

Page 4 of 5 FirstFirst 12345 LastLast

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