简体   繁体   English

在尝试比较两个代码的速度时,时间复杂度是唯一需要考虑的事情吗?

[英]Is time complexity the only thing to consider when trying to compare two codes in terms of terms their speed?

Recently i wrote a code for finding primefactors of a number within the limits of "int" datatype in C.最近,我编写了一个代码,用于在 C 中的“int”数据类型的范围内查找数字的素因子。 When i showed it to my friend he told that there are more optimised ways which have time complexities of O(sqrt(n)), O(log(n)).当我把它展示给我的朋友时,他告诉我有更优化的方法,它们的时间复杂度为 O(sqrt(n))、O(log(n))。 When I ran the code having time complexity O(sqrt(n)) and my code on Dev C++ i saw that the total time taken by both the codes was nearly the same.当我运行时间复杂度为 O(sqrt(n)) 的代码和我在 Dev C++ 上的代码时,我看到两个代码所花费的总时间几乎相同。 What is the time complexity of my code?我的代码的时间复杂度是多少?

#include <stdio.h>
int main(void)
{
    int k = 2, n, m;
    printf("enter a +ve integer greater than 1:- ");
    scanf("%d", &n);
    m = n;
    do {
        if (n % k == 0) {
            n /= k;
            printf("%d,", k);
        }
        if (n == 1)
            break;
        while (n % k != 0) {
            if (k == 2)
                k = 3;
            else
                k += 2;
        }
    } while (n % k == 0);
    printf("prime factors of %d\n.", m);
    return 0;
}

The time-complexity analysis is made for the sake of large N .为了N 大而进行时间复杂度分析。 If you do not feel the effect yet, your N is not large enough.如果您还没有感觉到效果,那么您的 N 还不够大。 I changed the %d s to %lld and int s to long long int and factored the square of the Mersenne prime that Euler found, ie (2³¹-1)².我将%d s 更改为%lld并将int s 更改为long long int并分解欧拉发现的梅森素数的平方,即 (2³¹-1)²。

Results:结果:

% echo 4611686014132420609 | time ./a.out
enter a +ve integer greater than 1:- 2147483647,2147483647,prime factors of 4611686014132420609
../a.out  12.15s user 0.00s system 99% cpu 12.154 total
% echo 4611686014132420609 | time factor
4611686014132420609: 2147483647 2147483647
factor  0.00s user 0.00s system 87% cpu 0.001 total

Ie even when compiled with GCC and -O3, your program required at least 12000 times as much CPU power to factor the number than the factor program.即,即使使用 GCC 和 -O3 编译,您的程序也需要至少 12000 倍的 CPU 功率来分解该数字,而不是factor程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 就大 O 而言,给定 function 的时间复杂度应该是多少? - What should be the time complexity of the given function in terms of Big O? 正则表达式与管道的速度 - Regex vs. Piping in terms of speed 有没有办法在运行时间上优化此C程序 - Is there a way to optimize this C program in terms of run time 如何使用 strtok 仅获取括号中的术语 - how to only get the terms in the bracket using strtok C中速度和空间消耗方面的静态与全局 - Static vs global in terms of speed and space consumption in C 循环遍历两个数组时最小化时间复杂度 - Minimizing time complexity when looping over two arrays 在等待互斥锁与等待信号量方面的CPU时间 - Waiting on mutex vs waiting on semaphore in terms of CPU-time 哪个函数在执行时间方面更快? beta() 还是 alpha()? - Which of the functions is faster in terms of the execution time? beta() or alpha()? 就处理内存而言,这两个C函数之间有什么区别? - What is the difference between these two C functions in terms of handling memory? 将几行代码写入一行是否有益(就内存和空间复杂性而言)。 这值得么? - Is it beneficial(in terms of memory & space complexity) to write a few lines of code into a single line. Is it worth it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM