Code: Select all
if(tries % 10000000 == 0){
cout << "Current tries: " << tries << "\r";
}
Code: Select all
if(tries % 10000000 == 0){
cout << "Current tries: " << tries << "\r";
}
I hear threading is complicated.. I haven't gotten quite that far yet.tarpman wrote:Without getting into threading, I doubt it.

Code: Select all
for( i=0; i < 10; i++)
{
for( tries=0; tries < 100000000; tries++)
{
do stuff
}
cout << "blah blah blah\r";
}

Eek! Boost zombies! BEGONE!!codergeek42 wrote:Maybe boost::timer would help with this?
Well, here is the current block. That's not exactly what I need..barophobia wrote:mod
branch if equal
That is alot of calculations!
Pipeline stalling is not a problem because branch prediction is usually going to be right.
Stop worrying about it.
you can be cute and do something likeCode: Select all
for( i=0; i < 10; i++) { for( tries=0; tries < 100000000; tries++) { do stuff } cout << "blah blah blah\r"; }
Code: Select all
for(int i = 0; i < repetitions; i++){
int tries = 0; // Keeping a running tab on the number of tries in the individual test.
do{ // Using do...while so the loop will execute at least once
findPhrase = kitty.giveString(); // Getting a string that has the number of letters that the user wants
tries++;
//if(tries % 10000000 == 0){
// cout << "Current tries: " << tries << "\r";
//}
}while(findPhrase != realPhrase); // If findPhrase is not equal to givenPhrase, then keep going
if(tries > maximumTries){ // These two if statements keep track of the minimum number of tries and the maximum number of tries
maximumTries = tries;
}
if(i == 0){
minimumTries = tries;
}else if(tries < minimumTries){
minimumTries = tries;
}
totalTries += tries;
cout << "It took " << setw(realPhrase.length() * 2) << tries << " tries to find the letters " << realPhrase << "." << endl; // We move the column over the length of the string * 2 so that it's all in a nice row.
}banned from #gentoo since sept 2017Neddyseagoon wrote:The problem with leaving is that you can only do it once and it reduces your influence.


Code: Select all
if ((tries & 0x3FFFFFF) == 0) {
cout << "Current tries: " << tries << "\r";
}
Oops forgot about the bitshift/and operation tricks.timeBandit wrote:]Code: Select all
if ((tries & 0x3FFFFFF) == 0) { cout << "Current tries: " << tries << "\r"; }
Code: Select all
next_update = N;
do{
findPhrase = kitty.giveString();
tries++;
if(tries >= next_update) {
cout << "Current tries: " << tries << "\r";
next_update = tries + N;
}
}while(findPhrase != realPhrase);Code: Select all
next_update = N;
do{
do{
findPhrase = kitty.giveString();
tries++;
}while((tries < next_update) && (findPhrase != realPhrase));
cout << "Current tries: " << tries << "\r";
next_update = tries + N;
}while(findPhrase != realPhrase);You also do not have to remember them: It should be sufficient to replace your number by a power of two - since this is a constant, any reasonable compiler will translate the mod into the corresponding and-operation if the processor can really do this faster.barophobia wrote:Oops forgot about the bitshift/and operation tricks.
Why? (Note that there is an && not an ||). On assembler level it translates into two tests anyway. At least the "straightforward" translation into assembler of the above code examples is identical (except that the second example has even an additional test in the outer loop).Akkara wrote:Roll the update with the exit condition. Testing two low-probability events simultaneously is better than one at a time.
Actually, it is more than just a mask, as seen with:It should be sufficient to replace your number by a power of two - since this is a constant, any reasonable compiler will translate the mod into the corresponding and-operation if the processor can really do this faster
Code: Select all
echo "int main(int ac,char **av) { return ac % 16; }" | cc -x c -S -O2 - -o - | sed -n '/^main/,/ret/p'A quick test...On assembler level it translates into two tests anyway.
Code: Select all
echo "int main(int ac,char **av) { return ac < 5 && av != 0; }" | cc -x c -S -O2 - -o - | sed -n '/^main/,/ret/p'Code: Select all
echo "int main(int ac,char **av) { return ac > 2 && av[1] != av[2]; }" | cc -x c -S -O2 - -o - | sed -n '/^main/,/ret/p'I was not aware that the standard declares mod by mathematical nonsense which is even harder to calculate. Last time I checked, this was implemented properly (but I admit that my last check is many many years ago). Usually, I don't have this problem since I don't use signed numbers - in most cases they are useless. One just has to get the habit to use e.g. the appropriate types for loop counters instead of the stupid "int". Of course, in the above case the proper solution would be also to give tries the correct (unsigned) type.Akkara wrote:The reason for that behavior, is way back when, someone had the brilliant idea that negative numbers modulo a positive divisor should return negative answers
I don't see the connection with the mod-sillyness: Implementation of division (be it on assembler or sub-assembler level) needs probably case distinctions anyway - so on the same level one could calculate a correct mod in the corresponding case.In (some) fairness however, this was sort-of forced because of the way integer division was defined to chop toward zero regardless of the sign of operands: integer a / b == sign(a) * sign(b) * floor(abs(a) / abs(b))
Thanks, now I understand what you mean. So the generated code is not what one would obtain by a "straightforward" translation of the C code: Statistically, it appears to be cheaper to do a test in vain than to have a branch (even although branch prediction might be correct). After thinking about it, this sounds reasonable....suggests that it generates only one branch.
I incorrectly used the word "standard" in my previous post, which is too strong a word to describe the situation.I was not aware that the standard declares mod by mathematical nonsense which is even harder to calculate.
Code: Select all
x: 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 ...
q = x/3: 3 2 2 2 1 1 1 0 0 0 0 0 -1 -1 -1 -2 -2 ...Code: Select all
x: 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 ...
q = x/3: 3 2 2 2 1 1 1 0 0 0 0 0 -1 -1 -1 -2 -2 ...
3 * y: 9 6 6 6 3 3 3 0 0 0 0 0 -3 -3 -3 -6 -6 ...
r = x - 3*q 0 2 1 0 2 1 0 2 1 0 -1 -2 0 -1 -2 0 -1 ...
Code: Select all
x: 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 ...
x % 3: 0 2 1 0 2 1 0 2 1 0 2 1 0 2 1 0 2
x - x%3: 9 6 6 6 3 3 3 0 0 0 -3 -3 -3 -6 -6 -6 -9
q 3 2 2 2 1 1 1 0 0 0 -1 -1 -1 -2 -2 -2 -3