[英]How do you count number of operations in java code , i'm not sure but can someone verify my work
Sum <- 0 // 1 Operation
for i <- 1 to n do // 2n operations
for j <-1 to n do // 2(n-1) operations
k <-1 // 1 operation
while k < n do // n-1 operations
k <- k *c // 2 operations
sum <- sum +1 // 2 operations
In total the number of operations in the code are : 该代码中的操作总数为:
1+2n+2(n-1)+1+(n-1)+2+2 == 5n+3 total # of operations , 1 + 2n + 2(n-1)+1+(n-1)+ 2 + 2 == 5n + 3个操作总数,
is this how you calculate it , because i understand the concept of each stmt has 3 portions to it ( Comparison, Assignment, Incrementation ) 这是您如何计算的,因为我理解每个stmt的概念都包含3部分(比较,分配,增量)
please feel free to correct me is my observations are incorrect 请随时纠正我,因为我的观察不正确
No, that's probably incorrect. 不,那可能不正确。
First of all: You're thinking too hard. 首先:您想得太辛苦了。 At least for the purposes of Big-O calculations, you can probably treat each assignment as a single operation, no matter whether it's a constant assignment or a calculated value. 至少出于Big-O计算的目的,您可以将每个赋值视为一个单独的操作,而不管它是常量赋值还是计算值。
Second of all: You aren't thinking hard enough. 第二:您没有认真思考。 The fourth line is a single operation, but it's run n * n
times, so it should be counted as n^2
, not 1
. 第四行是单个操作,但是它运行了n * n
次,因此应计为n^2
,而不是1
。 Similarly for other lines in loops. 对于循环中的其他行类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.