(07-27-2018, 04:47 AM)Ender Wrote: @ProfessorChill
If you aren't changing the length of the array in the loop, you shouldn't do this:
Code:
for (x = 0; x < s.length(); x++) {
It checks the length with each loop, wasting some cycles each time. Â Instead, just put the length in a variable ahead of time.
You also do this twice:
Code:
index[static_cast<int>(s[x])]
If you want ideal speed, you should just store that in a variable. Â It's not the most memory efficient, but it's better for CPU efficiency, especially when you're casting each time. Â Also, I don't think it's necessary to convert from char to int, just use char. Â Although interestingly, int variables are faster to deal with on some architectures, like ARM.
I just wanted to point out that though this might be true, it doesn't apply in every case. Compilers include optimizations to the code to make it more efficient. String lengths or data sizes are usually optimized into a local variable or register so something like this:
Code:
for (int i = 0; i < strlen(string); i++) {
  ...
}
may be compiled down such that the loop operates on a static value.
Another classic example that creates optimizations is:
Code:
int num = 0;
for (int i = 0; i < ONE_MILLION; i++) {
  num++;
}
may destroy the loop entirely and just statically initialize the num variable directly to its resulting value. This can create scenarios where higher level languages such as Java give the illusion that it is faster than a language such as C which preserved the loop.
So it depends on how your code is compiled. I'm assuming you submit your source and the server compiles the code for you using predefined optimizations so your binary does not get optimized as much.
Some things I've noticed when I compiled your code using no optimization flags:
- Yes, your code does constantly call string::length every time it iterates the loop,
- Since you are operating on the std:
tring data type, every time you use the bracket operators, you are calling the class's string::operator[] member,
- As above, every time you are using max or memset, it is calling std::max or std::memset,
To optimize the above, you can try:
- As Ender stated, you may store the string::length value into a variable to remove the string::length call every iteration,
- For the string::operator[], you may store the string into an array so that it can direct access without the need for the function call overhead,
- For the std::memset and std::max, you may also remove the call overheads by inlining them (I'm not 100% sure if this is recommended since the compiler should normally determine how to optimize this but do NOT create your own by using some basic data assignment to 0 using a byte-by-byte loop or something like that, there is actually proper optimization in the memset function call),
- Perhaps there is a way to initialize your index array without using std::memset such that when it is compiled, it will be initialized to 0 in the actual binary itself, thus removing any need to initialize it (for more info, read about the BSS segment),
- Multithreading for overall optimization.