How can one experience temporal complexity "experimentally"?

Can this be done by storing the counter to find out how many iterations go through the algorithm, or is it necessary to record the length of time?

+5
source share
6 answers

The complexity of the algorithm is defined as (something like :)

the number of operations that the algorithm performs as a function of its input size.

So, you need to try your algorithm with different input sizes (i.e. for sorting - try sorting 10 elements, 100 elements, etc.) and count each operation (for example, assignment, increment, mathematical operation, etc. ), which makes the algorithm.

"" .
, - profiling.

+4

, , . .

. ( , , ), .

, , . () (n ^ 2):

for n = 5 to 10000 //n: problem size
  long start = System.time()
  executeAlgorithm(n)
  long end = System.time()
  long totalTime = end - start

  double ratio = (double) time / (n * n)
end

. n , ...

  • ? . - (, n ^ 3)
  • ? . - (, nlogn)
  • ? ! ( , n, ).

O, f(x) = O(g(x)) <=> f(x) < c * g(x) - f(x) - , g(x) - , , c . f(x)/g(x); , c.

+9

"", . "" : , quicksort, .

, , , , .

+1

, cpu, . . , :

, , .

+1

.

, .

0

May I suggest using the ANTS profiler. He will provide you with such information while you run the application with "experimental" data.

0
source

All Articles