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. CString - high CPU usage

CString - high CPU usage

Scheduled Pinned Locked Moved C / C++ / MFC
announcement
9 Posts 6 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.
  • M Offline
    M Offline
    micutzu
    wrote on last edited by
    #1

    Hello, I have a class that logs string information into text files. I do a lot of string operations (using CString) and there are several instances of my class running in the same time. Everything works fine excepting high CPU usage which goes up to 70 – 80 percent when 100-150 instances are running. The final version of the project will require 300-400 instances running and the CPU usage should be as low as possible. If anyone can suggest me a CString replacement that will reduce the CPU usage, I would highly appreciate it. Thanks, Dan.

    J K D 3 Replies Last reply
    0
    • M micutzu

      Hello, I have a class that logs string information into text files. I do a lot of string operations (using CString) and there are several instances of my class running in the same time. Everything works fine excepting high CPU usage which goes up to 70 – 80 percent when 100-150 instances are running. The final version of the project will require 300-400 instances running and the CPU usage should be as low as possible. If anyone can suggest me a CString replacement that will reduce the CPU usage, I would highly appreciate it. Thanks, Dan.

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      First, see if you really have to use CString at all...  It is far too easy to get into the habit of abusing CString objects because so many developers are too familiar with them.    If you really do need to use a string object, you can try to reduce the number of allocate/deallocate cycles by keeping them around (i.e. do not cause creation of CString objects).    Preallocating space into them when they are first created may also improve performance.  See the CString::GetBufferSetLength(...) function for more info.    For most logging applications, you rarely need to log all that much data.  For example, a CString can hold easily hold more than 1 megabyte of data, but you will rarely be logging that much in a single call.  If you have a maximum logging amount, something like 4, 8, 16 or even 32KB, you can easily use a stack-based buffer for something as small as that.  This will significantly improve logging performance.    If you are logging large (>=4KB) amounts of data and using CString::Format(...) you are really wasting time due to the way CString::Format(...) works.  You are much better off managing your own memory or sticking to stack-based buffers and a function like ::_sntprintf(...).    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      See DeleteFXPFiles

      1 Reply Last reply
      0
      • M micutzu

        Hello, I have a class that logs string information into text files. I do a lot of string operations (using CString) and there are several instances of my class running in the same time. Everything works fine excepting high CPU usage which goes up to 70 – 80 percent when 100-150 instances are running. The final version of the project will require 300-400 instances running and the CPU usage should be as low as possible. If anyone can suggest me a CString replacement that will reduce the CPU usage, I would highly appreciate it. Thanks, Dan.

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        IMO, avoid using all forms of (xxx)printf, including CString.Format. I once hand-optimized a loop for writing magnetic tapes. I managed to decrease the processing time (for 1000000 simulated tape writes) from 21 seconds to 4. Just by replacing one sprintf statement with a number of strcat(), strcpy(), itoa() e.t.c A that time, I realized the cost (performancewise) of using (xx)printf. And as James R. Twine said, avoid memory allocations.

        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

        1 Reply Last reply
        0
        • M micutzu

          Hello, I have a class that logs string information into text files. I do a lot of string operations (using CString) and there are several instances of my class running in the same time. Everything works fine excepting high CPU usage which goes up to 70 – 80 percent when 100-150 instances are running. The final version of the project will require 300-400 instances running and the CPU usage should be as low as possible. If anyone can suggest me a CString replacement that will reduce the CPU usage, I would highly appreciate it. Thanks, Dan.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Preallocation, as James has already been mentioned, will greatly improve the performance. You might also consider using string instead of CString.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          G 1 Reply Last reply
          0
          • D David Crow

            Preallocation, as James has already been mentioned, will greatly improve the performance. You might also consider using string instead of CString.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            DavidCrow wrote:

            You might also consider using string instead of CString

            Is string actually faster? Do you know of any benchmark results? I've got a couple classes in an application that use CString heavily, and wonder if string would improve their performance.


            Software Zen: delete this;

            Fold With Us![^]

            D J 2 Replies Last reply
            0
            • G Gary R Wheeler

              DavidCrow wrote:

              You might also consider using string instead of CString

              Is string actually faster? Do you know of any benchmark results? I've got a couple classes in an application that use CString heavily, and wonder if string would improve their performance.


              Software Zen: delete this;

              Fold With Us![^]

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Gary R. Wheeler wrote:

              Do you know of any benchmark results?

              Nothing scientific, just empirical testing.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              1 Reply Last reply
              0
              • G Gary R Wheeler

                DavidCrow wrote:

                You might also consider using string instead of CString

                Is string actually faster? Do you know of any benchmark results? I've got a couple classes in an application that use CString heavily, and wonder if string would improve their performance.


                Software Zen: delete this;

                Fold With Us![^]

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                Gary R. Wheeler wrote:

                Is string actually faster? Do you know of any benchmark results? I've got a couple classes in an application that use CString heavily, and wonder if string would improve their performance.

                The memory allocation schemes are different between the two.  I believe (but I could be wrong) that the std::string object allocates like std::vector does - that is, it doubles each time.  8 characters, then 16 characters, then 32 characters, etc.    CString on the other hand, basically only allocates what it needs.  In MFC 6.0 release mode, CString will use the CPlex* allocators which allocate memory in fixed sized chunks, so that if you keep appending smaller strings or single characters to a CString it may not have to reallocate each time.    std::string can have its reference counting turned off (or it may be by default in some implementations), which may increase its performance a bit.    I guess it would depend on the usage of the string object.  If you are simply using them as replacements for a manually managed char buffer, and are not doing anything really specific to a string object with it, you may not notice much difference.  If you build strings through constant append operations, you may notice a difference.    If using a multithreaded VC++ 6.0 app, you can customize the allocator used for std::string much easier than CString, which will allow you to help mitigate heap contention between threads.  (Which can be a big hit if CString abuse is high.)    :P  IMHO, the best way you can increase CString performance is to avoid it entirely!  :P    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                S 1 Reply Last reply
                0
                • J James R Twine

                  Gary R. Wheeler wrote:

                  Is string actually faster? Do you know of any benchmark results? I've got a couple classes in an application that use CString heavily, and wonder if string would improve their performance.

                  The memory allocation schemes are different between the two.  I believe (but I could be wrong) that the std::string object allocates like std::vector does - that is, it doubles each time.  8 characters, then 16 characters, then 32 characters, etc.    CString on the other hand, basically only allocates what it needs.  In MFC 6.0 release mode, CString will use the CPlex* allocators which allocate memory in fixed sized chunks, so that if you keep appending smaller strings or single characters to a CString it may not have to reallocate each time.    std::string can have its reference counting turned off (or it may be by default in some implementations), which may increase its performance a bit.    I guess it would depend on the usage of the string object.  If you are simply using them as replacements for a manually managed char buffer, and are not doing anything really specific to a string object with it, you may not notice much difference.  If you build strings through constant append operations, you may notice a difference.    If using a multithreaded VC++ 6.0 app, you can customize the allocator used for std::string much easier than CString, which will allow you to help mitigate heap contention between threads.  (Which can be a big hit if CString abuse is high.)    :P  IMHO, the best way you can increase CString performance is to avoid it entirely!  :P    Peace!

                  -=- James
                  Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  See DeleteFXPFiles

                  S Offline
                  S Offline
                  Sceptic Mole
                  wrote on last edited by
                  #8

                  James R. Twine wrote:

                  std::string can have its reference counting turned off (or it may be by default in some implementations), which may increase its performance a bit.

                  See the comparison in http://www.codeproject.com/string/fix_str.asp[^]

                  J 1 Reply Last reply
                  0
                  • S Sceptic Mole

                    James R. Twine wrote:

                    std::string can have its reference counting turned off (or it may be by default in some implementations), which may increase its performance a bit.

                    See the comparison in http://www.codeproject.com/string/fix_str.asp[^]

                    J Offline
                    J Offline
                    James R Twine
                    wrote on last edited by
                    #9

                    Unless I am missing something, I am not seeing a good comparison there.  I see a few mentions of the words "fast" and "slow" but nothing that shows exactly how fast or slow something is.  Something has to be faster or slower than another.    Peace!

                    -=- James
                    Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    See DeleteFXPFiles

                    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