如何在python中检查程序的运行时间
我是python新手,我需要检查在python中运行代码的时间。 代码如下所示:如何在python中检查程序的运行时间,python,Python,我是python新手,我需要检查在python中运行代码的时间。 代码如下所示: np.random.seed(42) from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test=train_test_split(x_1,y_1,test_size=0.2) clf=SVC(kernel='rbf',probability=True) clf.fit(x_train,np.ravel(y_t
np.random.seed(42)
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x_1,y_1,test_size=0.2)
clf=SVC(kernel='rbf',probability=True)
clf.fit(x_train,np.ravel(y_train))
clf.score(x_test,y_test)
如果有任何建议,非常感谢 使用时间
import time
np.random.seed(42)
from sklearn.model_selection import train_test_split
start = time.time()
x_train,x_test,y_train,y_test=train_test_split(x_1,y_1,test_size=0.2)
clf=SVC(kernel='rbf',probability=True)
clf.fit(x_train,np.ravel(y_train))
clf.score(x_test,y_test)
end = time.time()
print("Time taken {}".format(end - start))
大体上
import time
start = time.time()
# do something
end = time.time()
print("Time taken {}".format(end - start))
您可以使用timeit python库。否则,在您的情况下,您可以使用以下代码
从时间导入性能计数器,睡眠
开始=性能计数器()
睡眠(2)
end=性能计数器()
打印(f'所需时间:{(结束-开始)}')
使用timeit
-使用monotonic()
或perf_counter()
而不是time()