Skip to main content
added 216 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

Since this code takes 1/1000 of a second of the 90 second total. It is contributing to 0.0001% of the execution time. This is so insignificant that you should not even be trying to optimize this part of the code.

My test code:

My test code:

Since this code takes 1/1000 of a second of the 90 second total. It is contributing to 0.0001% of the execution time. This is so insignificant that you should not even be trying to optimize this part of the code.

My test code:

added 210 characters in body
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>

int step[2] = { 0xAC, 0x723A };
int packageType = 1;

int computeLevel(int blockNum)
{
    int num = (blockNum / 0x70E4) * step[1]; // changed.
    if (blockNum < 0x70E4)
        return num + step[0];                // changed
    return (1 << packageType) + num;
}


// Add this to pre-vent the code being optimized to zero.
int result = 0;
int check  = 0;
int doComputeLevel(int blockNum)
{
    result += computeLevel(blockNum);
    ++check;
}


int main()
{
    // Set up random test data
    std::vector<int>    data;
    data.reserve(300010);
    for(int loop = 0;loop < 300000;++loop)
    {   
        data.push_back(rand());
    }   

    // Run the test
    clock_t t = clock();
    std::for_each(data.begin(), data.end(), doComputeLevel);
    clock_t e = clock();

    // print the results;
    std::cout << (e-t) << std::endl;
    std::cout << (e-t)*1.0/CLOCKS_PER_SEC << std::endl;
    std::cout << data.size() << " : " << check << " : " << result << std::endl;
} 

> g++ -O3 t.cpp
> ./a.out
1812                           TICKS
0.001812                       TIME in seconds
300000 : 300000 : 62755196     iterations : check of iterations : result
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>

int step[2] = { 0xAC, 0x723A };
int packageType = 1;

int computeLevel(int blockNum)
{
    int num = (blockNum / 0x70E4) * step[1]; // changed.
    if (blockNum < 0x70E4)
        return num + step[0];                // changed
    return (1 << packageType) + num;
}


// Add this to pre-vent the code being optimized to zero.
int result = 0;
int check  = 0;
int doComputeLevel(int blockNum)
{
    result += computeLevel(blockNum);
    ++check;
}


int main()
{
    // Set up random test data
    std::vector<int>    data;
    data.reserve(300010);
    for(int loop = 0;loop < 300000;++loop)
    {   
        data.push_back(rand());
    }   

    // Run the test
    clock_t t = clock();
    std::for_each(data.begin(), data.end(), doComputeLevel);
    clock_t e = clock();

    // print the results;
    std::cout << (e-t) << std::endl;
    std::cout << (e-t)*1.0/CLOCKS_PER_SEC << std::endl;
    std::cout << data.size() << " : " << check << " : " << result << std::endl;
}
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>

int step[2] = { 0xAC, 0x723A };
int packageType = 1;

int computeLevel(int blockNum)
{
    int num = (blockNum / 0x70E4) * step[1]; // changed.
    if (blockNum < 0x70E4)
        return num + step[0];                // changed
    return (1 << packageType) + num;
}


// Add this to pre-vent the code being optimized to zero.
int result = 0;
int check  = 0;
int doComputeLevel(int blockNum)
{
    result += computeLevel(blockNum);
    ++check;
}


int main()
{
    // Set up random test data
    std::vector<int>    data;
    data.reserve(300010);
    for(int loop = 0;loop < 300000;++loop)
    {   
        data.push_back(rand());
    }   

    // Run the test
    clock_t t = clock();
    std::for_each(data.begin(), data.end(), doComputeLevel);
    clock_t e = clock();

    // print the results;
    std::cout << (e-t) << std::endl;
    std::cout << (e-t)*1.0/CLOCKS_PER_SEC << std::endl;
    std::cout << data.size() << " : " << check << " : " << result << std::endl;
} 

> g++ -O3 t.cpp
> ./a.out
1812                           TICKS
0.001812                       TIME in seconds
300000 : 300000 : 62755196     iterations : check of iterations : result
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

The best optimization you can do is write code that works.

It makes it infinitely faster:

BYTE step[2] = { 0xAC, 0x723A };

    DWORD num = (blockNum / 0x70E4) * step[2];
                                //    ^^^^^^^   Broken. Undefined behavior.
                                //              The rest of the code is invalid.

Second I don't believe your numbers:

1 minute, 30 seconds for 300,000 calls.

That's an awfully slow machine. 90 seconds for 300,000 calls or 3,333 calls a second. Even unoptimized this code runs (300,000) in less then 1/100 of a second on my machine when fully optimized it is closer to 1/1000 of a second.

So it is not this code that is causing you to slow down. But the result of this code may be affecting the call path inside your application and causing other more costly code to be executed.

My test code:

#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>

int step[2] = { 0xAC, 0x723A };
int packageType = 1;

int computeLevel(int blockNum)
{
    int num = (blockNum / 0x70E4) * step[1]; // changed.
    if (blockNum < 0x70E4)
        return num + step[0];                // changed
    return (1 << packageType) + num;
}


// Add this to pre-vent the code being optimized to zero.
int result = 0;
int check  = 0;
int doComputeLevel(int blockNum)
{
    result += computeLevel(blockNum);
    ++check;
}


int main()
{
    // Set up random test data
    std::vector<int>    data;
    data.reserve(300010);
    for(int loop = 0;loop < 300000;++loop)
    {   
        data.push_back(rand());
    }   

    // Run the test
    clock_t t = clock();
    std::for_each(data.begin(), data.end(), doComputeLevel);
    clock_t e = clock();

    // print the results;
    std::cout << (e-t) << std::endl;
    std::cout << (e-t)*1.0/CLOCKS_PER_SEC << std::endl;
    std::cout << data.size() << " : " << check << " : " << result << std::endl;
}