Quote:
Originally Posted by HamletEagle
I am not saying your code is conplex, I am saying it has a bad complexity. The complexity can refer to how much memory the code uses(spacial complexity) or how many steps it does(temporal complexity).
When we want to know how fast a code is we estimate the temporal complexity.
In your code you have two loops, one inside the other.
When the first loop executes one time, the inner loop will execute n times, where n is the length of the string.
Therefore, for each iteration of the outer loop we have n iterations of the inner loop. However, the outer loop will execute n times too, which means in total your code.does at most n*n steps to find the answer.
Using 2 read_flags calls and comparing:
read_flags will traverse the string and calculate the bitsum. To traverse a string of length n the for will execute n times, so n steps.
We need to do read_flags 2 times so we do 2*n steps.
Then we have to compare the results of the 2 read_flags which we can do in one step(one ==).
We obtain that this method does 2*n + 1 steps. However, 1 is much "smaller" than n so we can ignore it.
2*n has the same asymptotic growth as n so the final complexity is n.
Doing n steps to get an answer is better than n^2. That is, if you really wanted to use the most efficient solution( as you said previously).
Because n is small(small strings), the difference may not be noticed.
That's how you would usually estimate the speed of your code. In amxx we also have to account for the pawn c++ interop time so a call to read_flags may be more expensive than n steps.
Note: when dealing with complexity we usually measure the worst case complexity(meaning the code cant do more stepts than that no matter what).
You have a break in the loops which may reduce the number of stepts the algorithm makes, but this should reflect in the best case complexity, not worst case.
|
I'm using a so-called nested loop, you probably know it's widely used in other programming languages, just to explain what the code does to someone who might be interested:
Let's say you have these flags "abcdefgh" and you need "abcdef" to earn a new rank.
PHP Code:
public containi_all( szStringSrc[ ], iSize1, szStringCompare[ ], iSize2 )
{
new bool:bEqual;
for( new i; i < iSize1; i++ )
{
for( new j; j < iSize2; j++ )
{
if( szStringSrc[ i ] == szStringCompare[ j ] )
{
bEqual = true;
break;
}
else
{
bEqual = false;
}
}
if( ! bEqual )
break;
}
return bEqual;
}
Basically it will loop through all of the String1, thus being the flags you need,
abcdef.
So it will go like, a, b, c, d, e, f.
Each time it goes through a character, let's say 'a', it will also check the entire String2 (your flags) to see if you have the flag, if it's true, it will return true and break the inner loop, then will continue to the second flag, being 'b'. And it will continue doing the same thing.
It will also work if you have flags 'abcdef' and you need 'badcef' which are basically the same flags with different order, normal contain/i won't.
__________________