-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.ps1
47 lines (40 loc) · 1.59 KB
/
time.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$source=@'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Timer
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetProcessTimes(IntPtr handle, out long creation, out long exit, out long kernel,
out long user);
public static void Time(string file,string args)
{
long user,kernel,exit,creation;
Process proc = null;
proc = Process.Start(file,args);
proc.WaitForExit();
GetProcessTimes(proc.Handle, out creation, out exit, out kernel, out user);
long real = exit - creation;
Console.WriteLine("real {0}\nuser {1}\nsys {2}", real / 10000000.0, user/10000000.0,kernel/10000000.0);
}
}
'@
Add-Type -TypeDefinition $source -Language CSharp
function time ($scriptblock) {
$file = "powershell";
$args = $scriptblock;
$startInfo = new-object Diagnostics.ProcessStartInfo;
$startInfo.FileName = $file;
$startInfo.Arguments = $args;
$startInfo.CreateNoWindow = $true;
$startInfo.UseShellExecute = $false;
$startInfo.RedirectStandardOutput = $true;
$process = [Diagnostics.Process]::Start($startInfo);
$process.WaitForExit();
write-host $process.StandardOutput.ReadToEnd();
write-host real: ($process.ExitTime - $process.StartTime)
write-host user: $process.UserProcessorTime;
write-host sys: $process.PrivilegedProcessorTime;
write-host using GetProcessTimes
#[Timer]::Time($file,$args)
}