Efficiency with c++

This is a discussion on Efficiency with c++ within the C++ Programming forums, part of the General Programming Boards category; at the risk of posting in the wrong board again (hopefully not) my question is regarding the efficiency of a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    Efficiency with c++

    at the risk of posting in the wrong board again (hopefully not) my question is regarding the efficiency of a program I've built. Ive never gone this delved this far before into programing so I dont know if there is even a solution round it. Here is the scenario.....
    I have my program and basically it creates a byte by byte copy of a pen drive. It works fine and i was happy with it until now. Whenever I run the program it hogs all my computers resources making it impossible to do anything else until my program has finished which in the past was fine until last nite when i urgently needed to check something as well which caused a major computer crash. Is improving efficiency of a program down to the way its coded of do i have to implement additional code and so on. Ive never looked at the efficiency before of any of my programs because they have never been so resource hungry as this one!

    any input will be greatly appreciated

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Basically, it consumes resources to make the process as fast as possible.
    If you want to keep it from doing that, add some sleeps. For Windows, you can use Sleep, for Linux, you can use sleep. This allows the OS to divert more processing power to other things.
    Also, you can lower the thread's priority, if that's possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Ah right, the OS my prog is based for is windows. Ive just had a quick browse at sleep and it sounds gud but i get the feeling if i implement this its going to dramatically slow down the time it takes to actually complete its task even though it would give me more processing time to do other things. I guess its a battle between processing power and performance but i really dont want to slow down the time it takes my prog to complete by too much if i can.

    At the risk of sounding stupid, can i lower a threads priority in windows? if so it sounds like it mite be worth having a look at?

    which would you guys think is the best route?




    EDIT: Ive just implemented a sleep10 into my processing loop. It has given me a lot of my processing power back as you said but the time its gunna take to image a 1gb thumb drive has dramatically increased. Even though ive got my processing power back im not sure it will outweigh the wait for my prog to complete
    Last edited by pastitprogram; 08-08-2008 at 05:55 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Yes, lowering thread/process priority is usually better.
    It means Windows will divert processing power to other tasks when needed only.
    You can set priority programmatically or via the task manager, the latter would probably be easier for now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm a bit surprised you manage to load Windows so badly that it causes a crash to do something else. Is your program using a lot of memory? If so, you may want to increase your swapfile's size.

    Aside from that, it sounds pretty wasteful to read byte by byte from the thumbdrive. It would probably be 10-100x more efficient to read blocks of between 512 and 8192 bytes at a time [I'm guessing on the ideal size, you may find the ideal size is slightly bigger, unlikely that smaller will be better]. Thumb drives are block-devices, so the content will not be an "unusual" number of bytes [but if you really worry about that, then you could just check how much the read function says it got back].

    Putting Sleep() or some similar function into your code will just make it run slower, it won't improve the applications efficiency. If you know [or likely expect] your application to take some time [like many minutes], then lowering the priority is a good way to increase the overall responsiveness of the system when running other processes - however, if your application is using a lot of memory, the responsiveness of the system is most likely regulated by the "swap" process, which is part of the OS itself, not your application, and you can't really change the priority of those this process. Using less memory is the right solution here.

    Finally, this subject has little to do with C++ and much to do with "how to write good applications", regardless of language.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Hi, when i said byte by byte, i meant 512 bytes at a time. I think i will increase it to 8192 at a time to see how that effects my overall process. Although it sounds complex i think i would like to lower the priority of my program (or at least give the option to). I ve found this

    http://www.mycplus.com/tutorial.asp?TID=289

    which seems to have everything i may need to achieve this task. I believe lowering a threads priority involves the following processes judging from that site.

    1: Get a handle on my apps process.
    2: set the process to whichever priority


    Im curious as to when you get a hold of the apps process would it be the executable name that appears in the task manager?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Is your program single threaded? If so, write the data in a separate thread so you can begin with the next read before the write is finished.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Is your program single threaded? If so, write the data in a separate thread so you can begin with the next read before the write is finished.
    You don't even need threads for that, just use "OVERLAPPED" IO - it makes life easier to just do that. Just remember that you need more than one buffer to read/write overlapped IO, since you can't be reading into the same buffer that is being written [or strange things will happen].

    Edit: To change your tasks priority, check out the example in MSDN SetPriorityClass.

    --
    Mats
    Last edited by matsp; 08-08-2008 at 06:54 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    Ive have 2 seperate buffers, 1 for reading and 1 for writing and i have just added (LPOVERLAPPED) to my code so hopefully everything should work out ok for that. Just so i get an understandiung of this "LPOVERLAPPED" creates a more efficient input/output operation, allowing both to happen at the same time rather than waiting to read, then waiting to write so on?

    Im going to check that example out now matsp

    thanks for this help everyone

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Although I really don't know what kind of program it is, I was thinking it's like a driver with real-time priority, which would make it steal all the processor power until it finishes.
    On a single processor, real-time priority can easily steal the processor time from all other applications and make the OS sluggish.

    And a hybrid read/write approach might make things slower too, depending on what type of disk it is. Especially on hard disks this applies, but I don't know about pen drives. Experiment!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pastitprogram View Post
    Ive have 2 seperate buffers, 1 for reading and 1 for writing and i have just added (LPOVERLAPPED) to my code so hopefully everything should work out ok for that. Just so i get an understandiung of this "LPOVERLAPPED" creates a more efficient input/output operation, allowing both to happen at the same time rather than waiting to read, then waiting to write so on?
    Correct - you do not wait for each operation to finish.

    Note that you can't just issue lots of overlapped reads/writes. You will need to issue one read, then issue a second read, wait for the first read to complete, then issue the write for the first read data. Then you start waiting for the second read, and then issue the write for the second dataset, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Whenever I run the program it hogs all my computers resources making it impossible to do anything else until my program has finished
    In those kind of case where you have a non-critical process running in background, you normally want the process to have an above normal priority. Here's another link about process/thread priorities on Windows http://msdn.microsoft.com/en-us/libr...00(VS.85).aspx

    Or since your application consist of mostly I/O operation (if not exclusively), you could also think of disabling priority boost, which happens when an I/O operation complete (I'm not sure but I guess it's still true with asynchronous I/O). I'm not sure if it would help. You could try experimenting and see what it gives.
    I hate real numbers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    right ive been having a mess around with it now and ive implemented some options to change its priority. By implementing it as below average priority, as long as im not hammering my computer with other processes and things the performance of my program seems to be quite good which im pleased with. I believe as well that the overlapped function into my read and write buffers and upping the buffer size has also speed the whole situation up as it appears to be going faster than i remember it doing. Thank you everyone for your help, it has been very much appreciated and given me a totally new insight into building further programs. u learn something new every day!

    cheers guys

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Although I really don't know what kind of program it is, I was thinking it's like a driver with real-time priority, which would make it steal all the processor power until it finishes.
    On a single processor, real-time priority can easily steal the processor time from all other applications and make the OS sluggish.

    And a hybrid read/write approach might make things slower too, depending on what type of disk it is. Especially on hard disks this applies, but I don't know about pen drives. Experiment!
    But it sounds like this program is I/O bound, not CPU bound. Would changing the priority make a difference for an I/O bound process?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    But it sounds like this program is I/O bound, not CPU bound. Would changing the priority make a difference for an I/O bound process?
    I agree with that. So there may not be much solution to the problem.

    I also notice that reading flash-cards [or thumb-drives], there are times when the whole system is very sluggish even if there's no CPU activity going on. This is most likely because the entire filesystem driver is waiting for the rather slow USB flash device to respond to a read/write request, and it can not handle another request until the flash one has finished.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating space efficiency using sizeof()
    By markcls in forum C Programming
    Replies: 6
    Last Post: 05-19-2007, 05:25 AM
  2. Efficiency problems
    By Crazy Glue in forum C# Programming
    Replies: 15
    Last Post: 07-20-2006, 08:38 AM
  3. Programme Efficiency
    By Cikotic in forum C Programming
    Replies: 3
    Last Post: 06-28-2004, 01:29 PM
  4. Binary tree search efficiency
    By ExCoder01 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-23-2003, 10:11 PM
  5. Algorithm Efficiency
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2002, 06:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21