可以编写一个测试函数,在函数中分别使用 (a+b)c 和 ac+b*c 进行计算并比较它们的计算速度。下面是一个 Python 代码示例:
import time
def test_speed(a, b, c):
start_time = time.time()
result1 = (a+b)*c
end_time = time.time()
time1 = end_time - start_time
print("(a+b)*c takes ", time1, " seconds")
start_time = time.time()
result2 = a*c + b*c
end_time = time.time()
time2 = end_time - start_time
print("a*c+b*c takes ", time2, " seconds")
if time1 < time2:
print("(a+b)*c is faster than a*c+b*c")
elif time1 > time2:
print("a*c+b*c is faster than (a+b)*c")
else:
print("(a+b)*c and a*c+b*c have the same speed")
# 测试
test_speed(1, 2, 3)
此代码将运行两次计算并记录每个计算的时间。最后,它将比较两个时间并打印计算速度更快的那个。