locked
Visual studio 2010 quick sort profiling RRS feed

  • Question


  • Hello,

    (VS 2010, x32)

    I wrote next code:

    vector<unsigned int> vec;

    // ... filling numbers with 100000 elements

    call sort with and without custom comparer see below

    1)

    std::sort(vec.begin(), vec.end());

    2)

    bool comp(unsigned int left, unsigned int right)
    {
       return left < right;
    }
    
    std::sort(vec.begin(), vec.end(), comp);

    Profiler shows a big difference on these two calls(I run profiler in release mode):

    Elapsed inclusive time for sort for 1 variant ~ 5.0,

    while for second variant this time ~ 1200.0.

    Difference is 240 times.

    After that I added time measurement StartCounter and GetCounter functions(see below) and wrapped calls to sort for both variants and build my app in release mode I have got another results. (4.88 for version without custom comparer and 8.18 for version with custom comparer).

    Difference is < 2 times.

    double PCFreq = 0.0;
    __int64 CounterStart = 0;
    
    void StartCounter()
    {
        LARGE_INTEGER li;
        if(!QueryPerformanceFrequency(&li))
            cout << "QueryPerformanceFrequency failed!\n";
    
        PCFreq = double(li.QuadPart)/1000.0;
    
        QueryPerformanceCounter(&li);
        CounterStart = li.QuadPart;
    }
    double GetCounter()
    {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return double(li.QuadPart-CounterStart)/PCFreq;
    }

    Is it normal behaviour for Profiler for Instrumentation mode?

    Or maybe i am measuring time of sort execution incorrectly?

    Here is full code:

    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <Windows.h>
    using namespace std;
    double PCFreq = 0.0;
    __int64 CounterStart = 0;
    
    void StartCounter()
    {
        LARGE_INTEGER li;
        if(!QueryPerformanceFrequency(&li))
            cout << "QueryPerformanceFrequency failed!\n";
    
        PCFreq = double(li.QuadPart)/1000.0;
    
        QueryPerformanceCounter(&li);
        CounterStart = li.QuadPart;
    }
    double GetCounter()
    {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return double(li.QuadPart-CounterStart)/PCFreq;
    }
    
    // function generator:
    int RandomNumber () { return (rand()%100); }
    
    bool comp(unsigned int left, unsigned int right)
    {
       return left < right;
    }
    
    int main()
    {
       vector<unsigned int> vec1(100000), vec2;
       generate (vec1.begin(), vec1.end(), RandomNumber);
       copy(vec1.begin(),vec1.end(), back_inserter(vec2));
    
       {
          StartCounter();
          sort(vec1.begin(),vec1.end(), comp);
          std::cout << GetCounter() << endl;
       }
    
       {
          StartCounter();
          sort(vec2.begin(), vec2.end());
          std::cout << GetCounter() << endl;
       }
    
       return 0;
    }





    • Edited by leon1705 Friday, October 12, 2012 1:22 PM
    Thursday, October 11, 2012 12:49 PM

All replies

  • Hi leon1705,

    Just to make this issue clearly, do you mean that you are using the VS performance Wizard to profile the app in VS? Would you mind sharing us the whole app? We will try to profile it in our PC.

    Please also attach your Visual Studio project, you can upload it to the sky driver, and then share the download link in your post. Please also attach a screenshot image about the result you get in your post.

    If possible, please also show the detailed steps about how you profile your app. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 15, 2012 8:25 AM
    Moderator
  • Hi Jack,

    Yes I used VS performance Wizard to profile the app in VS.

    Here is full code:

    #include "stdafx.h"
    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <Windows.h>
    using namespace std;
    typedef std::vector<unsigned int> UIntVector;
    double PCFreq = 0.0;
    __int64 CounterStart = 0;
    
    void StartCounter()
    {
       LARGE_INTEGER li;
       if(!QueryPerformanceFrequency(&li))
          cout << "QueryPerformanceFrequency failed!\n";
    
       PCFreq = double(li.QuadPart)/1000.0;
    
       QueryPerformanceCounter(&li);
       CounterStart = li.QuadPart;
    }
    double GetCounter()
    {
       LARGE_INTEGER li;
       QueryPerformanceCounter(&li);
       return double(li.QuadPart-CounterStart)/PCFreq;
    }
    
    // function generator:
    int RandomNumber () { return (rand()%100); }
    
    bool comp(unsigned int left, unsigned int right)
    {
       return left < right;
    }
    
    void SortWithCustomComparer(UIntVector& vec)
    {
       sort(vec.begin(),vec.end(), comp);
    }
    
    void SortWithDefaultComparer(UIntVector& vec)
    {
       sort(vec.begin(), vec.end());
    }
    
    int main()
    {
       UIntVector vec1(100000), vec2;
       generate (vec1.begin(), vec1.end(), RandomNumber);
       copy(vec1.begin(),vec1.end(), back_inserter(vec2));
    
       StartCounter();
       SortWithCustomComparer(vec1);
       std::cout << "Quick sort with custom comparer(pointer to function): " <<GetCounter() << endl;
    
       StartCounter();
       SortWithDefaultComparer(vec2);
       std::cout << "Quick sort with default comparer: " << GetCounter() << endl;
    
       return 0;
    }

    Here is results when I just run application:

    sdrv.ms/XBqXYz

    Here is results from profiler:

    sdrv.ms/XBr2M1

    the first call to sort is for version with custom comparer, while the second is for version with default comparer.

    Here is the link for the project:

    sdrv.ms/RdcBdr

    Steps to reproduce:

    1) Without profiler:

        1. build app in release mode

        2. press F5

    2) With profiler:

        1. Press menu Analyze -> Launch Performance Wizard... -> select Instrumentation and press Next -> Select project QuickSortProfiling -> Select Launch profiling after the wizard finishes, press Finish button.

        2. After profiling session will be finished, select Function Details View in profiler report, and switch performance metric to Elapsed Inclusive Time.

        3. Open main function metrics.

    Sorry for the links, antispam system does not allow me to insert normal images and hyperlinks.

    Thanks,

    leon1705



    • Edited by leon1705 Saturday, October 20, 2012 7:52 AM
    Saturday, October 20, 2012 7:52 AM
  • Hi leon1705,

    Thank you for posting in the MSDN forum.

    Maybe you could share us the results what you get.

    I try to repro it in my PC, and I get the results like the following screen shot.

    Not the VC++ expert, so I’m not very sure about what will impact the result in your app, one idea is that you could use the process monitor to check the .dll files when you run your app and profile it with the profiling tool, and find the differences. I doubt that it will impact the results.

    About the Function Performance Details, see this MSDN document.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 22, 2012 9:32 AM
    Moderator
  • Hi Jack,

    I've shared my results in previous post:

    Here is results when I just run application:

    http://sdrv.ms/XBqXYz

    Here is results from profiler:

    http://sdrv.ms/XBr2M1

    the first call to sort is for version with custom comparer, while the second is for version with default comparer.

    Here is the link for the project:

    http://sdrv.ms/RdcBdr


    As you see on second picture the difference between profiling function sort is huge, while on first picture it is ~2 times.

    Thanks,

    leon1705

    Monday, October 22, 2012 10:31 AM
  • Hi leon1705,

    Sorry for delay.

    I try to repro it in my PC, but I get the similar result as yours.

    I just find that the .dll files could impact the result, you could open the Modules window when your debug it, and then make sure that all .dll files loaded normally.

    In addition, I’m not very sure that whether the result in the console window is the whole result as the profile tool. To help you resolve this issue, I’m afraid that these questions require a more in-depth level of support. I suggest you visit the following link to see the various paid support options that are available to better meet your needs.http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Thanks for your understanding.

    Sincerely,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 24, 2012 2:27 AM
    Moderator
  • Hi Jack,

    I am sorry for delay,

    had a hunch that profiler does not make the same steps which I made in my example, but I was wonder about the difference order ~ 240 times.

    Thanks for your help Jack.

    Saturday, November 3, 2012 6:10 PM