Unusual Speed Difference between Python and C++

后端 未结 17 2294
庸人自扰
庸人自扰 2020-12-22 21:25

I recently wrote a short algorithm to calculate happy numbers in python. The program allows you to pick an upper bound and it will determine all the happy numbers below it.

17条回答
  •  醉酒成梦
    2020-12-22 21:38

    Other optimizations: by using arrays and direct access using the loop index rather than searching in a vector, and by caching prior sums, the following code (inspired by Dr Asik's answer but probably not optimized at all) runs 2445 times faster than the original C++ code, about 400 times faster than the Python code.

    #include 
    #include 
    #include 
    
    void calcMain(int upperBound, std::vector& known)
    {
        int tempDigitCounter = upperBound;
        int numDigits = 0;
        while (tempDigitCounter > 0)
        {
            numDigits++;
            tempDigitCounter /= 10;
        }
        int maxSlots = numDigits * 9 * 9;
        int* history = new int[maxSlots + 1];
    
        int* cache = new int[upperBound+1];
        for (int jj = 0; jj <= upperBound; jj++)
        {
            cache[jj] = 0;
        }
    
        int current, sum, temp;
        for(int i = 0; i <= upperBound; i++)
        {
            current = i;
            while(true)
            {
                sum = 0;
                temp = current;
    
                bool inRange = temp <= upperBound;
                if (inRange)
                {
                    int cached = cache[temp];
                    if (cached)
                    {
                        sum = cached;
                    }
                }
    
                if (sum == 0)
                {
                    while (temp > 0)
                    {
                        int tempMod = temp % 10;
                        sum += tempMod * tempMod;
                        temp /= 10;
                    }
                    if (inRange)
                    {
                        cache[current] = sum;
                    }
                }
                current = sum;
                if(history[current] == i)
                {
                    if(current == 1)
                    {
                        known.push_back(i);
                    }
                    break;
                }
                history[current] = i;
            }
        }
    }
    
    int main()
    {
        while(true)
        {
            int upperBound;
            std::vector known;
            std::cout << "Pick an upper bound: ";
            std::cin >> upperBound;
            long start, end;
            start = GetTickCount();
            calcMain(upperBound, known);
            end = GetTickCount();
            for (size_t i = 0; i < known.size(); ++i) {
                std::cout << known[i] << ", ";
            }               
            double seconds = (double)(end-start) / 1000.0;
            std::cout << std::endl << seconds << " seconds." << std::endl << std::endl;
        }
        return 0;
    }
    

提交回复
热议问题