# How can I optimize this for speed? (C++)

Here is my method:

// can change
DWORD step = { 0xAC, 0x723A };
packageType = 1;

DWORD computeLevel(DWORD blockNum)
{
DWORD num = (blockNum / 0x70E4) * step;
if (blockNum < 0x70E4)
return num + step;
return (1 << packageType) + num;
}


I am trying to optimize this for speed, as it gets called about 300,000 times and one little operation could add alot of time on. If I change the blockStep to just constant values, this makes it much, much faster. The problem is, is that they can change so I cannot make it const. Also, if I just have it return a straight up 0, no comparisons, it saves my code around 1 minute, 30 seconds, so this function is really slowing me down.

Also, it would be nice if this function could also be enhanced. I already tried by best, but it would be great if it can get better.

DWORD computeLevel0(DWORD blockNum)
{
WORD quotient1 = (blockNum / 0xAA);
BYTE quotient2 = (blockNum / 0x70E4);
DWORD num = quotient1 * 0xAC;

// optimizations (0 comparison)

WORD mult1 = quotient1;
mult1 |= mult1 >> 8;
mult1 |= mult1 >> 4;
mult1 |= mult1 >> 2;
mult1 |= mult1 >> 1;
mult1 &= 1;

BYTE mult2 = quotient2;
mult2 |= mult2 >> 4;
mult2 |= mult2 >> 2;
mult2 |= mult2 >> 1;
mult2 &= 1;

num += ((quotient2 + 1) << packageType) * mult1;
num += (((1 << packageType)) * mult2) * mult1;

return num;
}

• Are there any characteristics of blockNum that we need to be aware of? Occurs sequentially with no gaps? Essentially random on each call? Only in a specific range? Jul 30 '12 at 6:19
• blockNum will always be less than 0xFFFFFF. Yes, it is pretty much random on each call. Jul 30 '12 at 6:29
• I don't think any of your hand optimizations are actually providing you with a benefit. This kind of macro optimization is ultimately self defeating and usually leads to slower code as you confuse the compiler optimizer which is a million times better than you at optimizations. Jul 30 '12 at 7:54
• After my analysis (see below). This code is contributing to 0.0001% of the execution time. Any time spent trying to optimize this is a complete and utter waste of time. Write it as clearly as possible without regard for optimization. Anything you do will have ZERO affect on the run time. Jul 30 '12 at 8:07
• There are at least 2 bugs in your code (initializing byte to value that is too large for the type and accessing the step array out of bounds). Fix those bugs first. Jul 30 '12 at 8:37

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

It makes it infinitely faster:

BYTE step = { 0xAC, 0x723A };

DWORD num = (blockNum / 0x70E4) * step;
//    ^^^^^^^   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.

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:

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

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

int computeLevel(int blockNum)
{
int num = (blockNum / 0x70E4) * step; // changed.
if (blockNum < 0x70E4)
return num + step;                // 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

• It also calls other methods 300,000 times. I'm just saying that's how much it takes off of the whole process, as there are many other functions being called, not just this one. Jul 30 '12 at 7:56
• @hetelek: I understand this. It is your other functions that are taking the time not this one. Making this return 0 just means you execute another path through the other code that is less expensive. This code is not causing any significant cost. Jul 30 '12 at 7:58
• Would it help if I posted all the functions? Jul 30 '12 at 8:13
• It would probably help you a lot to learn to use the profiling tools provided by MS. They will point you at any bottlenecks that need optimizations. The most affective optimizations are done at the algorithm level not at the micro what instruction should I use level. Jul 30 '12 at 8:18
• And the other broken thing: Initializing BYTE to 0x723A, which is too large for the type. Jul 30 '12 at 8:35

I'll expand on this later when I get a chance, but something immediately jumps out at me:

DWORD num = (blockNum / 0x70E4) * step;
if (blockNum < 0x70E4)


You could rearrange this and potentially save a few operations:

DWORD computeLevel(DWORD blockNum)
{

if (blockNum < 0x70E4) {
//if blockNum < 0x70e4, (blockNum / 0x70e4) == 0, thus num == 0.
return step;
}

DWORD num = (blockNum / 0x70E4) * step;

return (1 << packageType) + num;

}


Depending on compiler optimization, how the CPU pipelines, and what the bytecode generated is, this might not make a difference, but it's worth a try. Also, if that branch isn't taken very often, this isn't going to make a significant difference.

• Thanks for the repsonse, but I cannot do that. If (blockNum < 0x70E4) is true, than it return 'num + step', so 'num' must be calculated before. Jul 30 '12 at 7:13
• @hetelek But num is guaranteed to be 0 in that situation. For any two ints, x, y, if x < y, x / y = 0. Like 3/5, or 100/101. For all z, 0 * z = 0, thus, if the condition is true, num = 0. Jul 30 '12 at 7:18
{"ft_lang_label":"__label__en","ft_lang_prob":0.80229914,"math_prob":0.95269513,"size":1377,"snap":"2021-43-2021-49","text_gpt3_token_len":420,"char_repetition_ratio":0.14348143,"word_repetition_ratio":0.0,"special_character_ratio":0.3543936,"punctuation_ratio":0.16793893,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97222275,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-21T06:29:44Z\",\"WARC-Record-ID\":\"<urn:uuid:72d8097c-98d1-44bc-8793-f74fd3041a82>\",\"Content-Length\":\"195753\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1b514583-a1ff-4681-ab09-db7a298be89f>\",\"WARC-Concurrent-To\":\"<urn:uuid:402ddedb-5fdd-422a-ad39-d254fd073c50>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/14133/how-can-i-optimize-this-for-speed-c\",\"WARC-Payload-Digest\":\"sha1:P44BQDTKTOKBN2HWLR6A3VM55KBFCJFI\",\"WARC-Block-Digest\":\"sha1:675ZCXYYVYWCTEG7MJ6O7ACP4KOD7RHL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585381.88_warc_CC-MAIN-20211021040342-20211021070342-00127.warc.gz\"}"}