More CPU friendly code

Dear experts,

In my thread I have a method with the code below. My application is launching a lot of these threads for decoding text blocks (each block is about 10 MB). The problem is that all these threads are consuming up to 98% of the CPU.

My question, if there is a more CPU friendly method than the code I use to reduce the CPU load:

[code]dim d as integer = data.size -1 // data is a MemoryBlock
dim p as Ptr = data // Ptr to the MemoryBlock

For i As Integer = 0 To d
p.byte(i) = p.byte(i)-42
Next

return data
[/code]

Secondly, I have my doubts if for-while-loops in threads are a wise thing to do ?!?

PS. I simplified the code to make it more readable.

Thank you.

well, if you want to reduce cpu usage, you also reduce speed of the code.

like this:
in the loop you do

if bitwiseAnd(i, &hFFFFF) = 0 then
me.sleep(10)
end if

so you have to find how much iterations to jump above before sleeping and than how much to sleep.

Might be related to a bug in thread use which triggers high CPU - see <https://xojo.com/issue/29327>

https://forum.xojo.com/5068-2013-r3-is-not-usable-for-production-apps-for-me/

This bug is too severe. I hope that Xojo this time consider a dot release soon.

Thanks guys, I hope this issue is related to the bug indeed. I am pulling me hear out here for the last few days :frowning:

I’m confused.

In the Op’s original post he stated he is running many computationally intensive threads and it’s using 98% of the CPU.
Isn’t this to be totally expected?

Jim

R3 introduced a bug making some programs consummate all the CPU power. Check the thread. So, the user must check his program behavior with R2 before making sure his program is really so CPU intense as pointed. Using the sleep(10) as pointed by Christian usually would decrease the CPU consumption, but now, probably will just trigger the bug.

I just tried the project with R2, but unfortunately the results are the same as with R3.
So I come back with the initial question, is there is a more CPU friendly method than the code I use written above to reduce the CPU load.

Thank you.

Why do you want to reduce the CPU load? If you are processing data, I would think you want it to use as much CPU as possible so that it finishes its task quicker.

With that said, you could try tweaking the Priority value of your threads to give more CPU to other tasks (such as the main UI thread).

Paul, I tried the to lower priority, but that didn’t help.
The problem with the high CPU load is that the GUI is not responding flawless and I prefer a better GUI response above the processing time.

I would test it this way:

#pragma DisableBackgroundTasks // let us control thread yielding
dim d as integer = data.size -1 // data is a MemoryBlock
dim p as Ptr = data // Ptr to the MemoryBlock
  
For i As Integer = 0 To d 
   p.byte(i) = p.byte(i)-42
   if i % 10000 = 0 then    // test various values of this to get nice threading behavior
      me.Sleep(2)   // test various values of this to get nice threading behavior
   end if
Next
return data

The goal is to control how often the thread yields, and for how long it yields, so that it’s giving enough time to the Main thread.

Your example code is rather simple. Is there anything else in the thread that could be blocking it? Plugin usage? Synchronous Shell? As Michael suggests, perhaps your code is not yielding for some reason.

I would recommend running the same code in an older framework (say 12r2.1) and comparing the CPU usage. I’ve also noticed that threads cause a large jump in CPU use in 13r3 compared to 12r2.1 with no inherent increase in application performance.

Michael found a jump from R2 to R3. No need to go so far to find problems if you’ve read this thread: https://forum.xojo.com/5068-2013-r3-is-not-usable-for-production-apps-for-me/