python中for循环计算和矩阵计算速度对比、time计时方法

小数据量,for循环花费时间很少!
大数据量,矩阵运算花费时间很少!
python的for循环比matlab更高效更快速!

计算一个分段函数f(x)= , x>1; 1 , -1<x<=1;3+2x , x<=-1
可参考在matlab中:https://blog.csdn.net/qq_43328166/article/details/108270104

import time
import numpy as np
import pandas as pd

# 计时方法1:
# start1 = time.clock()
# 函数
# end1 = time.clock()
# time1 = end1-start1
# print('函数使用时间:',time1)

# 计时方法2:
# start1 = time.process_time()
# 函数
# end1 = time.process_time()
# time1 = end1-start1
# print('函数使用时间:',time1)


def for_calculate1(x):
    n = len(x)
    y = x
    for index in range(0,n-1):
        if x[index]>1:
            y[index] = x[index]**2
        elif -1<x[index]<=1:
            y[index] 

猜你喜欢

转载自blog.csdn.net/qq_43328166/article/details/108285468