Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Programmatic Function Profiling

Programmatic Function Profiling

Scheduled Pinned Locked Moved C / C++ / MFC
c++cssdebuggingperformance
27 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Varghese Paul M
    wrote on last edited by
    #1

    Hi All, Is there a way to find out the time taken by a function to complete (in terms of nanoseconds) in my c++ code without using "Function Profiling" feature of the linker. I tried GetTickCount(), but it gives the time in milliseconds. It always returns 0 because my function completes its task less than in one millisecond. Thanks in advance

    CPalliniC H R S enhzflepE 5 Replies Last reply
    0
    • V Varghese Paul M

      Hi All, Is there a way to find out the time taken by a function to complete (in terms of nanoseconds) in my c++ code without using "Function Profiling" feature of the linker. I tried GetTickCount(), but it gives the time in milliseconds. It always returns 0 because my function completes its task less than in one millisecond. Thanks in advance

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You may use the QueryPerformanceFrequency [[^](http://msdn.microsoft.com/en-us/library/ms644905.aspx "New Window")], QueryPerformanceCounter [^] pair. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      C 1 Reply Last reply
      0
      • V Varghese Paul M

        Hi All, Is there a way to find out the time taken by a function to complete (in terms of nanoseconds) in my c++ code without using "Function Profiling" feature of the linker. I tried GetTickCount(), but it gives the time in milliseconds. It always returns 0 because my function completes its task less than in one millisecond. Thanks in advance

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        Does this[^] helpful?

        1 Reply Last reply
        0
        • CPalliniC CPallini

          You may use the QueryPerformanceFrequency [[^](http://msdn.microsoft.com/en-us/library/ms644905.aspx "New Window")], QueryPerformanceCounter [^] pair. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          This can only be applied if there's thread switching: you are measuring the time before the function and then after the function and assume that the difference is the time spent in the function. While this might seem correct, it's not always the case: the scheduler can decide in the middle of your function to switch to another thread, which will 'pause' your function for a certain amount of time. Thus you will take that time into account also, which is wrong. A better approach would be to measure the time really spent for this thread. But unfortunately, I don't know how to do this (but I guess it is possible).

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          CPalliniC 1 Reply Last reply
          0
          • C Cedric Moonen

            This can only be applied if there's thread switching: you are measuring the time before the function and then after the function and assume that the difference is the time spent in the function. While this might seem correct, it's not always the case: the scheduler can decide in the middle of your function to switch to another thread, which will 'pause' your function for a certain amount of time. Thus you will take that time into account also, which is wrong. A better approach would be to measure the time really spent for this thread. But unfortunately, I don't know how to do this (but I guess it is possible).

            Cédric Moonen Software developer
            Charting control [v1.4] OpenGL game tutorial in C++

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            I knew that. I provided only a way to measure time on a smaller scale. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            C R 2 Replies Last reply
            0
            • V Varghese Paul M

              Hi All, Is there a way to find out the time taken by a function to complete (in terms of nanoseconds) in my c++ code without using "Function Profiling" feature of the linker. I tried GetTickCount(), but it gives the time in milliseconds. It always returns 0 because my function completes its task less than in one millisecond. Thanks in advance

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              This blog post[^] may give you some insights. I doubt if there's a ready made API that can give results at nano second precision. RDTSC[^] can be a good thing to bet upon, but don't take my word for it. :~

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

              CPalliniC 1 Reply Last reply
              0
              • CPalliniC CPallini

                I knew that. I provided only a way to measure time on a smaller scale. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                CPallini wrote:

                I knew that.

                Yeah I guess so. I was mainly clarifying for the OP :)

                Cédric Moonen Software developer
                Charting control [v1.4] OpenGL game tutorial in C++

                CPalliniC 1 Reply Last reply
                0
                • CPalliniC CPallini

                  I knew that. I provided only a way to measure time on a smaller scale. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  I agree with you. There can be several downsides to any and every method that can be suggested here, but there's no single fool proof way of doing it, or at least that I don't know of one. What if the method which is supposed to measure the time elapsed takes more time to execute than the actual set of instructions whose execution time is to be measured? :doh: It becomes more bizarre as we go down to get more precision.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                  CPalliniC 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    This blog post[^] may give you some insights. I doubt if there's a ready made API that can give results at nano second precision. RDTSC[^] can be a good thing to bet upon, but don't take my word for it. :~

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Rajesh R Subramanian wrote:

                    RDTSC[^] can be a good thing to bet upon, but don't take my word for it.

                    On old-fashioned-single-core-machines. ;P :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    R 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      I agree with you. There can be several downsides to any and every method that can be suggested here, but there's no single fool proof way of doing it, or at least that I don't know of one. What if the method which is supposed to measure the time elapsed takes more time to execute than the actual set of instructions whose execution time is to be measured? :doh: It becomes more bizarre as we go down to get more precision.

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Rajesh R Subramanian wrote:

                      What if the method which is supposed to measure the time elapsed takes more time to execute than the actual set of instructions whose execution time is to be measured?

                      A philosophical approach to quantum mechanics? :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      R 1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        Rajesh R Subramanian wrote:

                        RDTSC[^] can be a good thing to bet upon, but don't take my word for it.

                        On old-fashioned-single-core-machines. ;P :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        But does that apply only to RDTSC? :)

                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                        CPalliniC 1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          Rajesh R Subramanian wrote:

                          What if the method which is supposed to measure the time elapsed takes more time to execute than the actual set of instructions whose execution time is to be measured?

                          A philosophical approach to quantum mechanics? :-D

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          R Offline
                          R Offline
                          Rajesh R Subramanian
                          wrote on last edited by
                          #12

                          I had to re-read my post to realise why did you make a comment like that. :laugh: Sometimes, the philosopher in me wakes up. :-\

                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            CPallini wrote:

                            I knew that.

                            Yeah I guess so. I was mainly clarifying for the OP :)

                            Cédric Moonen Software developer
                            Charting control [v1.4] OpenGL game tutorial in C++

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #13

                            Well, indeed your was a good point against profiling-do-it-yourself. The OP should also be aware that between milli and nano there are micro seconds (i.e. pretending nano seconds accuracy it is at present, a bit Utopian!) :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            In testa che avete, signor di Ceprano?

                            1 Reply Last reply
                            0
                            • R Rajesh R Subramanian

                              But does that apply only to RDTSC? :)

                              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                              CPalliniC Offline
                              CPalliniC Offline
                              CPallini
                              wrote on last edited by
                              #14

                              Yes, as far as this article [^] is correct, the QueryPerformanceFrequency,QueryPerformanceCounter pair is more robust (as Windows API is expected to be). :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              In testa che avete, signor di Ceprano?

                              R 1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                Yes, as far as this article [^] is correct, the QueryPerformanceFrequency,QueryPerformanceCounter pair is more robust (as Windows API is expected to be). :)

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                R Offline
                                R Offline
                                Rajesh R Subramanian
                                wrote on last edited by
                                #15

                                I accept, I had not taken into account the fact that more than one processor core can exist, but: What QueryPerformanceCounter actually does is up to the HAL (with some help from ACPI). The performance folks tell me that, in the worst case, you might get it from the rollover interrupt on the programmable interrupt timer. This in turn may require a PCI transaction, which is not exactly the fastest thing in the world. It's better than GetTickCount, but it's not going to win any speed contests. In the best case, the HAL may conclude that the RDTSC counter runs at a constant frequency, so it uses that instead.[^] :)

                                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                CPalliniC 1 Reply Last reply
                                0
                                • R Rajesh R Subramanian

                                  I accept, I had not taken into account the fact that more than one processor core can exist, but: What QueryPerformanceCounter actually does is up to the HAL (with some help from ACPI). The performance folks tell me that, in the worst case, you might get it from the rollover interrupt on the programmable interrupt timer. This in turn may require a PCI transaction, which is not exactly the fastest thing in the world. It's better than GetTickCount, but it's not going to win any speed contests. In the best case, the HAL may conclude that the RDTSC counter runs at a constant frequency, so it uses that instead.[^] :)

                                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                  CPalliniC Offline
                                  CPalliniC Offline
                                  CPallini
                                  wrote on last edited by
                                  #16

                                  Rajesh R Subramanian wrote:

                                  In the best case, the HAL may conclude that the RDTSC counter runs at a constant frequency, so it uses that instead.

                                  That's API robustness. You should make the (in)famous QueryPerformanceFrequency/QueryPerformanceCounter pair make the best (while still reliable) choiche for you. ;)

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                  [My articles]

                                  In testa che avete, signor di Ceprano?

                                  R T 2 Replies Last reply
                                  0
                                  • CPalliniC CPallini

                                    Rajesh R Subramanian wrote:

                                    In the best case, the HAL may conclude that the RDTSC counter runs at a constant frequency, so it uses that instead.

                                    That's API robustness. You should make the (in)famous QueryPerformanceFrequency/QueryPerformanceCounter pair make the best (while still reliable) choiche for you. ;)

                                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    R Offline
                                    R Offline
                                    Rajesh R Subramanian
                                    wrote on last edited by
                                    #17

                                    CPallini wrote:

                                    You should make (in)famous QueryPerformanceFrequency/QueryPerformanceCounter pair make the best (while still reliable) choiche

                                    Agreed. :) Hijack attempt starts: How are you doing with the Connoisseur's tea? Liking it?

                                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                    CPalliniC 1 Reply Last reply
                                    0
                                    • R Rajesh R Subramanian

                                      CPallini wrote:

                                      You should make (in)famous QueryPerformanceFrequency/QueryPerformanceCounter pair make the best (while still reliable) choiche

                                      Agreed. :) Hijack attempt starts: How are you doing with the Connoisseur's tea? Liking it?

                                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #18

                                      Rajesh R Subramanian wrote:

                                      Hijack attempt starts:

                                      Hijack attempts goes stright on: Yes I like it. But I had few occasions to taste it again. It's quite hot these days here in Italy and I prefer lower temperatures to have cups of tea. (of course I can't miss hot coffee, but, you know, I'm an addict...) :-D

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                      [My articles]

                                      In testa che avete, signor di Ceprano?

                                      R 1 Reply Last reply
                                      0
                                      • CPalliniC CPallini

                                        Rajesh R Subramanian wrote:

                                        Hijack attempt starts:

                                        Hijack attempts goes stright on: Yes I like it. But I had few occasions to taste it again. It's quite hot these days here in Italy and I prefer lower temperatures to have cups of tea. (of course I can't miss hot coffee, but, you know, I'm an addict...) :-D

                                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                        [My articles]

                                        R Offline
                                        R Offline
                                        Rajesh R Subramanian
                                        wrote on last edited by
                                        #19

                                        CPallini wrote:

                                        I prefer lower temperatures to have cups of tea.

                                        Urgent?

                                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                        CPalliniC T 2 Replies Last reply
                                        0
                                        • R Rajesh R Subramanian

                                          CPallini wrote:

                                          I prefer lower temperatures to have cups of tea.

                                          Urgent?

                                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                                          CPalliniC Offline
                                          CPalliniC Offline
                                          CPallini
                                          wrote on last edited by
                                          #20

                                          Yes, plz plz. Or gimme more drugz! :-D

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          In testa che avete, signor di Ceprano?

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups