|
-
Feb 20th, 2013, 07:30 AM
#1
Thread Starter
Fanatic Member
Make 100% CPU Usage
Hi Guys!
Now I want something weird. Normally the opposite is desired but I need the 100% of the CPU. I have a very small code for a form uses high amounts of array for a mathematical computation.
When I compile it to exe, I want to make it use 100% of CPU during calculation.
Anyway to do it?
Thanks!
-
Feb 20th, 2013, 08:02 AM
#2
Re: Make 100% CPU Usage
Call DoEvents in a For..Next loop. What does your code do?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Feb 20th, 2013, 08:26 AM
#3
Thread Starter
Fanatic Member
Re: Make 100% CPU Usage
It is calculating n'th prime number. For example if you want the 3rd prime number (n = 3 - 1 because the arrays starts from 0), then:
It first starts with p(0) = 2 and checks if the next integer (in this case it is 3) divisible with p(0). If not, redims p() and it takes 3 to p(1).
Then next integer 4. This time, it checks if it is divisible with all p() values (p(0) and P(1)). Yes, it is divisible with p(0) so it skips 4.
And then next integer, 5. Checks if it is divisible again with all p() values. No, then, redims p() and it takes 5 to p(2). Now, p(n) meets the first condition (n = 2) and it stops.
This works fast when n is < 100000. But whenever I look for 1000000th prime, it takes a minute to solve. When open the task manager, I saw that it only uses the half of the CPU. I want to make it running faster.
That's the whole deal...
Last edited by Flashbond; Feb 20th, 2013 at 08:32 AM.
-
Feb 20th, 2013, 08:33 AM
#4
Re: Make 100% CPU Usage
Well then, you need a faster algorithm. Have you searched Google for a better solution?
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Feb 20th, 2013, 08:41 AM
#5
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Feb 20th, 2013, 08:42 AM
#6
Thread Starter
Fanatic Member
Re: Make 100% CPU Usage
How are you with coding? Can you optimize it for me if I send the code?
Last edited by Flashbond; Feb 20th, 2013 at 08:58 AM.
-
Feb 20th, 2013, 08:46 AM
#7
Thread Starter
Fanatic Member
Re: Make 100% CPU Usage
This one is nice, thanks!!
-
Feb 20th, 2013, 09:01 AM
#8
Thread Starter
Fanatic Member
Re: Make 100% CPU Usage
Code:
Private Sub Command1_Click()
Dim k As Long
Dim p() As Long
Dim n As Long
Dim m As Long
Dim l As Long
n = 1
m = 3
k = Me.Text1.Text
ReDim p(n)
p(0) = 2
If k = 1 Then
Beep
Me.Text2.Text = 2
Else
Do Until n = k
Do Until l = n
If Not m Mod p(l) = 0 Then
l = l + 1
Else
l = 0
m = m + 1
End If
Loop
ReDim Preserve p(n)
p(n) = m
n = n + 1
m = m + 1
l = 0
Loop
Beep
Me.Text2.Text = p(n - 1)
End If
End Sub
Text1 is the n'th prime input box. Text2 is the result box.
-
Feb 20th, 2013, 10:51 AM
#9
Re: Make 100% CPU Usage
 Originally Posted by Flashbond
When open the task manager, I saw that it only uses the half of the CPU.
Do you have a dual-core processor? (on the Performance tab of Task Manager, you will have two graphs for "CPU Usage")
I would guess so, in which case VB6 cannot use more than one core (for dual-core, that is 50%) without a large amount of extra work. VB.Net (VB 2002 and later) can do it relatively easily.
 Originally Posted by Flashbond
ReDim Preserve p(n)
That is something you should avoid having in a loop, because it is extremely slow - I wouldn't be surprised if well over 90% of the time your code takes is just that one statement.
Rather than starting with one item and adding one item at a time, start with a reasonable estimate of how many you will need (perhaps 1000?), and when you need to add more increase the size by a chunk each time (tests have shown that 25% of the current total generally works best).
Note that as the Long data type use 4 bytes, 1 MB of memory (which is a very small amount these days!) can store over 250000 items, so you shouldn't worry about being too precise with your memory usage.
It takes slightly more code (as you need to work out when the ReDim will run, and how much to increase the size by), but the speed difference should be very significant.
Last edited by si_the_geek; Feb 20th, 2013 at 10:55 AM.
-
Feb 20th, 2013, 11:17 AM
#10
Re: Make 100% CPU Usage
 Originally Posted by si_the_geek
D
Rather than starting with one item and adding one item at a time, start with a reasonable estimate of how many you will need (perhaps 1000?), and when you need to add more increase the size by a chunk each time (tests have shown that 25% of the current total generally works best).
Very good advice
I am surprised how often I see people using redim preserve in a loop often starting at 0 and calling every time sometimes even when they already have a count of what they would need even before the first item is written into the array.
Extending the array in chunks as described above is by far the best way to go and can make a huge difference in a loop that builds a large array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|