@
yuelang85 @
liprais 这种代码有啥好说的?既然你要看,那就给你:
python3:
def sum():
i = 0
s = 0
start = time.time()
while i < 1000000000:
i+= 1
s += i
end = time.time()
print("sum: {0}, use time: {1}'s".format(
s,
end-start))
sum()
java:
public class Sum {
public static void main(String[] args) {
long startTime=System.currentTimeMillis(); //获取开始时间
long i = 0, sum = 0;
while (i++ < 1000000000){
sum += i;
}
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.format("sum: %d, use time: %f's", sum,
(endTime - startTime)/1000.0);
}
}
c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main(){
struct timeval start, end;
gettimeofday(&start, NULL);
long long i = 0, sum = 0;
while (i++ < 1000000000){
sum += i;
}
gettimeofday(&end, NULL);
printf("sum: %lld, time used: %f 's",
sum,
end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1000000.0);
}