Page 1 of 1 1
Topic Options
#76746 - 2003-09-17 11:07 PM Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
I'm in the process of writing a script that goes through a file line by line and parses it. I have an onscreen counter that increments each time it goes through a line, that is where I use the $LineCounter variable. The problem is that it spikes the cpu up to 100% while the counter is running. Is there a command like a vb DoEvents that will keep this from happening?

Here's is the piece of my code that opens the file and increments the counter. The $PVFFile variable is filled earlier in the program..The WriteToLogSuccessful is later in the program. While the counter is running it is not writing anything to a log...
code:
If Open(1,"E:\Invoices\PVF\$PVFFile") = 0
$UseridFound = "N"
$LineOfText = ReadLine(1)
While @Error = 0
$LineCounter = $LineCounter + 1
If InStr($LineofText,"USERID") <> 0
$UseridFound = "Y"
$Userid = SubStr("$LineOfText",8,25)
$Userid = Trim($Userid)
$Hour = SubStr(@Time,1,2)
$Min = SubStr(@Time,4,2)
$Sec = SubStr(@Time,7,2)
$UseridFileName = "$Userid@MonthNo@MDayNo@Year$Hour$Min$Sec.txt"
$WriteEmailUserIni = WriteProfileString("E:\Invoices\Email\EmailUser.ini","$UserEmailFile","User","$Userid")
$LogText = "Found userid $Userid, created $UseridFileName"
Gosub WriteToLogSuccessful
EndIf
AT(4,0) "Lines read - $LineCounter "
$LineOfText = ReadLine(1)
Loop
EndIf

Thanks in advance...

Top
#76747 - 2003-09-17 11:18 PM Re: Counter kills cpu
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Why are you surprised about that? KiXtart just tries to run the script as fats as possible and thus grabs all CPU power available. You could alowasy create a kiXforms script with a timer that then parses the script.
_________________________
There are two types of vessels, submarines and targets.

Top
#76748 - 2003-09-17 11:21 PM Re: Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
Would it still use all the cpu if I put it in a kixform and did a doevents between the counters?
Top
#76749 - 2003-09-17 11:31 PM Re: Counter kills cpu
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
how 'bout sleep 0.001 ... How many times does it loop ?
_________________________



Top
#76750 - 2003-09-17 11:33 PM Re: Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
It depends on how many lines are in the file. The first test had over 35,000 lines in it...
Top
#76751 - 2003-09-17 11:44 PM Re: Counter kills cpu
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
that'd give an extra 35 seconds of execution time ... not that good, ay ?
_________________________



Top
#76752 - 2003-09-18 09:57 AM Re: Counter kills cpu
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You should probably think again about *why* you want to do this.

The purpose of "doevents" in VB and similar languages is not to release CPU to other process, it is specfically to allow event handling threads in the same process to execute. These threads may be blocked while a piece of code is executing. For instance, you may update a counter on a web form, but it won't display because the thread which handles the display update cannot execute until your function exits. In this case, "doevents" will allows the thread to execute.

The Windows kernels use scheduling tactics to ensure that all processes get a bit of the CPU. The scheduler will allocate more CPU to certain processes depending on their permissions, priority and the way that they execute.

All this means that if you are giving up CPU in a running process you are simply wasting time that your script could be processing. A CPU not running at 100% is a wasted CPU.

NB One caveat to this, the Win9x kernels are poorly designed as far as process handling, and can perform appallingly where you have a process hog.

The correct solution if you are worried about processor load is to give the job a lower priority.

There are a few ways to do this, but the easiest is using the DOS "START" command with the "/LOW" or "/BELOWNORMAL" switch. This will allow your script to have as much CPU as it wants where available, but the scheduler will give it a smaller piece of the CPU pie when there is contention.

[ 18. September 2003, 09:58: Message edited by: Richard H. ]

Top
#76753 - 2003-09-18 03:57 PM Re: Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
Thanks for all the replies...

Richard, based on what you're saying using a doevents command in the script will not make the system run more efficiently? I thought a system shouldn't be maxed out at 100%... Although the machine that it's running on is actually dedicated to that one task...

Top
#76754 - 2003-09-18 04:01 PM Re: Counter kills cpu
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so you should consider even setting it to "/high"
_________________________
!

download KiXnet

Top
#76755 - 2003-09-18 04:14 PM Re: Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
That won't kill the cpu and make it run dog slow?
Top
#76756 - 2003-09-18 05:27 PM Re: Counter kills cpu
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
cmarti, my two cents is that you simply make a call as to what is more important to you:

1) Finishing the task as soon as possible

2) Allow one to multi-task on this machine while the task is running.

If the first is more important to you, stick with optimized code, a tight loop and maybe play with priority a little bit - no sleep() timer.

If the second is more important, use a sleep timer of (0.001) and bite it on the extra time penalty.

If Kixtart had an builtin DoEvents() function, I would use that instead of sleep. The extra overhead associated with calling a COM DoEvents is way too expensive (imho).

[ 18. September 2003, 17:28: Message edited by: Shawn ]

Top
#76757 - 2003-09-18 05:42 PM Re: Counter kills cpu
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
quote:
Richard, based on what you're saying using a doevents command in the script will not make the system run more efficiently?
Correct. In fact it (probably) forces a context switch, which make the machine run less efficiently.

quote:
I thought a system shouldn't be maxed out at 100%
100% is exactly what you want your system to run at - that is good use of resources. What's the point of buying a 2GH processor, and using the same processing cycles of a 1.5GH processor?

The same thing applies to memory. Unused memory is wasted memory and means your machine is running efficiently. Any memory not used by in-core processes should be used by the kernel for i/o buffering.

You probably only have one CPU, which means that you can only have a single task running at any one time. Each process gets a timeslice in which it can run after which it gets kicked off the CPU. Both the length of time it gets to run and the frequency that it gets a slot are determined by the scheduler based on a number of parameters, of which one is the priority.

An over-burdened machine is one where processes have to wait excessively long for a scheduled slot, and the degree that you are "out of CPU" is entirely down to how you feel the programs are responding, not the percentage of time that the CPU is busy.

Ignoring i/o issues, a program will always take exactly the same time to run. However that time will be split up, and the more programs it is sharing the CPU with, the more time it will appear to take.

Imagine you have a time machine, and you jump forward a year every birthday. Now after doing this 10 times, you will have aged 10 years, but everyone else will have aged 20. The 10 years is like the CPU time required to run a program, the 20 years is like the "real" time that has passed. Objective and subjective, if you prefer.

quote:
so you should consider even setting it to "/high"
quote:
That won't kill the cpu and make it run dog slow?
The CPU cannot run slow - it is governed by a clock and it runs at that speed. The subjective time that a program takes to run can lengthen when a CPU has a lot to do.

You can make inefficient use of the CPU, mainly by unnecessary i/o.

Setting the process priority to high will give it a longer slice of time on the CPU and/or more slices than normal priority processes. It won't take any more or less CPU however.

If this is a machine dedicated to the task then it is appropriate to give it a high priority, although I'd probably avoid the "realtime" priority otherwise some system processes may not get a look in making the system unstable.

If it is a general purpose machine then you will want to lower the priority because you want the interactive elements (keyboard, mouse clicks, screen updates) to have a higher priority than the batch task. This is because usually you don't care if it takes an additional 10 seconds to run, but you do care if your computer becomes unresponsive while it is running.

As an aside, your CPU is *always* 100% busy, but the spare CPU cycles are consumed by a process called "System Idle Process" in task manager.

This is quite a simplified overview, and is a reasonable description for small machines where the interaction between processes and hardware components is not too significant.

Top
#76758 - 2003-09-18 05:45 PM Re: Counter kills cpu
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Oh and the other thing, a task like this might
best be run from the task scheduler than anywhere else. Plus, you might find that by not
writing status messages to the console, you will
save a whole whack of time. Writing to the
console is thought to be a resource pig at the
best of times anyways ...

-Shawn

Top
#76759 - 2003-09-18 06:05 PM Re: Counter kills cpu
cmarti Offline
Hey THIS is FUN

Registered: 2001-02-26
Posts: 297
Loc: Little Rock, AR
Thanks for all of the info. You all have been very helpful.

Based on what Richard is saying I will leave the program just as it is and let the system handle the cpu resources.

Shawn, I have the console counter so I can actually tell that it's doing something and not just stuck in the middle of a task. [Big Grin]

Thanks for all of the help eveyone..... [Cool]

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 1300 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.067 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org