Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

my powershell script uses a lot of processor time.
i see peaks of 100% each minute, the script runs each minute.
any tips to make it less processor hungry ?

this is the code:

$input_path = ‘C:\mango\web\system\*.tmp’
$fileDirectory = 'C:\mango\web\system'
$output_file = ‘C:\mango\web\A\output.txt’

Clear-Content $output_file
Clear-Content C:\mango\web\A\output1.txt
Clear-Content C:\mango\web\A\output2.txt

foreach($file in Get-ChildItem $fileDirectory )

{
    $filePath = $fileDirectory + "\" + $file;
    $A=(get-content $filePath -totalcount 3)[-1]
    $B=get-content $filePath -tail 1  
    Add-Content $output_file $A 
    $parts = $B.split(";")
    Add-Content $output_file $parts[1]
}
$yesterday =(Get-Date).date.AddDays(-1) 
Get-ChildItem C:\logs\ -recurse | 
Get-ChildItem | where { $_.CreationTime.Date -eq $yesterday } | get-content -tail 1 | Add-Content c:\mango\web\A\output.txt

$c=(Get-Content $output_file) -Join ";"
Add-Content C:\mango\web\A\output1.txt $c

(get-content C:\mango\web\A\output1.txt) -replace ',',"." | Add-content C:\mango\web\A\output2.txt


thanks in advance
Posted
Updated 1-Nov-15 13:42pm
v3
Comments
PIEBALDconsult 1-Nov-15 19:44pm    
<snide>What part of "Powershell" didn't you understand?</snide>

(I never use Powershell.)
Afzaal Ahmad Zeeshan 2-Nov-15 4:23am    
Can you share the processor or system details also?
Member 12083305 2-Nov-15 14:12pm    
Intel celeron M450 2.00Ghz 2Gb RAM

1 solution

There is no obvious solution.
Quote:
my powershell script uses a lot of processor time.
i see peaks of 100% each minute, the script runs each minute.
Looks pretty normal to me. PowerShell do what you say as quick as possible.

What you want is an optimisation, it is almost a job by itself, it is about being clever and avoiding brute-force.

To optimize, apart from most obvious cases, one need context. The context is about how many files, files size, what you want to do ...

The tool of choice is a profiler (when available).

For your problem, one practically need to be in front of your computer to see where is the problem.

Your previous question was fitting in the most trivial cases, not this time.

[Update]
Quote:
There is one part that could be smarter i think, but i dont know how.
Bad idea !
Smarter code doesn't matter if it is not where you spend time.
To reduce runtime, you need to know where you spend time, this is the job of a profiler to tell you that. If you don't have a profiler, you can log the time it takes to execute each part. the place where you spend time is the one you need to optimise.
 
Share this answer
 
v2
Comments
Member 12083305 2-Nov-15 14:15pm    
There is one part that could be smarter i think, but i dont know how.
Its this part:
(get-content C:\mango\web\A\output1.txt) -replace ',',"." | Add-content C:\mango\web\A\output2.txt

Here i open a txt file and replace , for . and write it into another file.
I use 3 output files in my script, ill think tah can be less, but i realy dont know how to do that.
Patrice T 2-Nov-15 17:49pm    
see solution update

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900