I have a large binary file (700 Mb approximately) which I load to TMemoryStream. After that I perform the reading with TMemoryStream.Read() and make some simple calculations but the application never takes more than 20% of CPU. My PC has i7 processor. Is there any chance to increase the CPU using and speed up the reading process without using the threads?
-
4Reading process is almost certainly limited by hard drive speed. I also wonder could the bandwidth of memory limit the speed, if your calculations are very simple? – Harriv Jun 19 '12 at 15:51
-
2There's quite possible a number of things that could be done to speed up your program. But without knowing any more than the fact that you call TMemoryStream.Read, it's impossible to offer any meaningful advice. – David Heffernan Jun 20 '12 at 11:41
-
3100% CPU Usage on ONE CORE = 25% CPU usage. A core i7 has 4 real and then "hyperthreading" makes it look like 8 cores to the operating system. That's 8 cores. I'd be happy you get more than 14% usage. If you were using 8 threads each fully CPU bound, you would get "100%" in task manager. Personally, I find a single-thread that is 100% cpu bound should have been reported by windows to be "100% cpu bound". Your code is already maximally inefficient in a single thread. – Warren P Jun 20 '12 at 12:34
2 Answers
As far as I know, the only way to utilise the power of multiple cpu cores with Delphi is to use threads.
If you do choose to use threads in your application, there are a couple libraries that may ease development. How Do I Choose Between the Various Ways to do Threading in Delphi?

- 1
- 1

- 9,649
- 7
- 44
- 75
Adding on to Shannon's answer, on an i7 processor with multiple cores, one thread will only be utilizing one core. One thread cannot run on more than one processor core. Therefore, if you wish to utilize multiple cores, you need to create multiple threads to handle various tasks. Creating a thread isn't necessarily as simple as saying do this in that thread, there's a lot to know about multi-threading. For example, your application has one main GUI thread, then one thread might be dedicated for performing some long calculation, another thread might be updating a caption to real-time data, and so on.
Windows automatically decides which core to assign a thread to, and usually divides it up fairly. So, if you have 8 processor cores, and 16 threads, each core would get 2 threads (presumably) and since each core sends its own ticks apart from each other, more than one thread could literally be running at the same time (as opposed to a single core where it divides each 'tick' between each thread).
So to answer your question, if you had 5 threads performing something big at the same time, then you would see 100% processor usage.

- 26,858
- 31
- 155
- 327
-
2
-
1However windows reports "25% CPU" in task manager when the single thread uses 100% of a single core. That is what Jerry is trying to explain here. – Warren P Jun 20 '12 at 22:05