我想测量代码中当前的经过时间以及多个代码段之间的经过时间。一个例子:
当我想获取经过的时间时
static void Main(string[] args)
{
Stopwatch _stopwatch = new Stopwatch();
_stopwatch.Start();
... do something ...
_stopwatch.Stop();
Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
}
我还可以像这样在执行代码期间输出经过的时间
static void Main(string[] args)
{
Stopwatch _stopwatch = new Stopwatch();
_stopwatch.Start();
... do something ...
Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
... do something ...
Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
... do something ...
Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
... do something ...
Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
// finally
_stopwatch.Stop();
Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
}
但是我想要的是这样的:
Code startet ... | elapsed time 00:02:01:sss | this single segment took 00:02:01:sss
Connected to database ... | elapsed time 00:02:04:sss | this single segment took 00:00:03:sss
Added 40.000 rows ... | elapsed time 00:05:23:sss | this single segment took 00:03:19:sss
所以我想要的是除了具有总的经过时间,而在代码中为单个部分停止的时间是与经过时间旁边的单个部分的时间一样。
我该怎么办?
答案 0 :(得分:0)
在每经过一个时间后,使用from sklearn.preprocessing import StandardScaler
class TabularDataset(Dataset):
def __init__(self, data, cat_cols=None, output_col=None):
self.n = data.shape[0]
# [...]
if output_col:
scaler_y = StandardScaler()
self.y = data[output_col].astype(np.float32).values.reshape(-1, 1)
scaler_y.fit(self.y)
self.y = scaler_y.transform(self.y)
# [...]
if self.cont_cols:
scaler_X_cont = StandardScaler()
self.cont_X = data[self.cont_cols].astype(np.float32).values
scaler_X_cont.fit(self.cont_X)
self.cont_X = scaler_X_cont.transform(self.cont_X)
# [...]
秒表方法;它将经过时间重置为0 ...并保留一个变量,以将经过时间总和作为总经过时间。
希望这对您有帮助...
答案 1 :(得分:0)
我想我解决了。对我来说,也许有些专家可以检查“优化”?
我将最后一个时间范围存储在一个可评估的值中,并从当前值中减去最后一个时间:
private static TimeSpan _pastTimeSpan;
static void Main(string[] args)
{
_stopwatch = new Stopwatch();
_stopwatch.Start();
_pastTimeSpan = _stopwatch.Elapsed;
... code ...
MyElapsedTime(_stopwatch.Elapsed);
... code ...
MyElapsedTime(_stopwatch.Elapsed);
... code ...
MyElapsedTime(_stopwatch.Elapsed);
_stopwatch.Stop();
Console.WriteLine("Time elapsed total: {0:hh\\:mm\\:ss}", _stopwatch.Elapsed);
}
private static void MyElapsedTime(TimeSpan ts)
{
// Get the last TimeSpan
TimeSpan pastTimeSpan = _pastTimeSpan;
// Update last TimeSpan with current
_pastTimeSpan = ts;
// Get difference between two
TimeSpan diffTs = ts.Subtract(pastTimeSpan);
Console.WriteLine(string.Format("Elapsed time {0}:{1} | Segment took {2}:{3}", Math.Floor(ts.TotalMinutes), ts.ToString("ss\\.ff"), Math.Floor(diffTs.TotalMinutes), diffTs.ToString("ss\\.ff")));
}
输出将是
Elapsed time 0:01.70 | Segment took 0:01.70
Elapsed time 0:01.78 | Segment took 0:00.07