Home Dashboard Directory Help
Search

GetTickCount not advancing by Robert Wishlaw


Status: 

Closed
 as Won't Fix Help for as Won't Fix


1
0
Sign in
to vote
Type: Bug
ID: 772117
Opened: 11/24/2012 11:04:10 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

///////////////////////////////////////////////////////////////////
//
// When the code below is compiled with
//
// Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
//
// and linked with
//
// Microsoft (R) Incremental Linker Version 8.00.50727.762
//
// and then run, the output, as expected, is
//
// 1822965
// 1823636
// strlen() time = 671 String length = 926
//
// When compiled with
//
// Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
//
// and linked with
//
// Microsoft (R) Incremental Linker Version 10.00.40219.01
//
// and run the output, not as expected, is
//
// 1839470
// 1839470 <----- The problem is that the GetTickCount has not advanced
// strlen() time = 0 String length = 926
//
// The compiler and flags are
// cl.exe /c /Ox /Gd /W1 /EHsc /MT /D_WIN32_IE=0x0501
// The linker and flags are
// link.exe /RELEASE /MACHINE:IX86 /SUBSYSTEM:CONSOLE
//
// The problem also exists with the compiler linker in Visual Studio Express for Desktop 2012.
//
//
// Robert Wishlaw
// irwishlaw@hotmail.com
//
///////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include <string.h>

char a[] =
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe:\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.\n"

"Beware the Jabberwock, my son!\n"
"The jaws that bite, the claws that catch!\n"
"Beware the Jubjub bird, and shun\n"
"The frumious Bandersnatch!\n"

"He took his vorpal sword in hand:\n"
"Long time the manxome foe he sought -\n"
"So rested he by the Tumturn tree,\n"
"And stood awhile in thought.\n"

"And, as in uffish thought he stood;\n"
"The Jabberwock, with eyes of flame,\n"
"Came whiffling through the tulgey wood,\n"
"And burbled as it came!\n"

"One, two! One, two! And through and through\n"
"The vorpal blade went snicker-snackl\n"
"He left it dead, and with its head\n"
"He went galumphing back.\n"

"And hast thou slain the Jabberwock?\n"
"Come to my arms, my beamish boy!\n"
"O frabjous day! Callooh! Callay!\n"
"He chortled in his joy.\n"

"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe:\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.";

static int     x;
static int     t;
static int     tt;
static int     kk;

int main(int argc, char *argv[])
{
tt= GetTickCount();
printf("% d\n",tt);
for(x=1; x<=1000000; x+=1)
{
    kk= strlen( a);
}

t= GetTickCount();
printf("% d\n",t);
t-= tt;
printf("%s% d%s% d\n","strlen() time = ",t," String length = ",kk);
return 0; /* End of main program */
}

Details
Sign in to post a comment.
Posted by Mike Danes on 11/26/2012 at 10:26 PM
"I would like to mention that strnlen and strnlen_s, which are CRT functions like strlen, work as expected. I don't understand why there is such discrimination within the CRT for the optimizer removing the for loop."

Well, the optimizer can't possible know every CRT function.

"I realize that having a strlen in a for loop is less than optimal in most code"

Having a strlen in a for loop is not more or less optimal, the problem is that your for loop does the same thing every iteration. It's unclear why would you do this anyway.
Posted by Robert Wishlaw on 11/26/2012 at 2:49 PM
Hi Mike:

Again, I thank you for the explanations and I would like to mention that strnlen and strnlen_s, which are CRT functions like strlen, work as expected. I don't understand why there is such discrimination within the CRT for the optimizer removing the for loop.

Hi Microsoft:

My original concern arose when some old code that worked with the compiler from Visual Studio 2005 would not work with the compilers from Visual Studio 2010 and 2012.

I realize that having a strlen in a for loop is less than optimal in most code but why is the for loop not removed for strnlen and strnlen_s? Anyway it appears that this behavior is by design with improvements in the optimizer so I would consider that this is resolved.

Thank you for your help.
Posted by Microsoft on 11/26/2012 at 12:01 AM
Hi Robert, have you resolved this?
Posted by Mike Danes on 11/25/2012 at 10:54 PM
"Thank you for the explanation of the bug."
Well, it's not a bug :)

"Other than strlen I wonder what other functions cause the compiler to eliminate the loop?"
Pretty much all functions that the compiler can prove that they do not use/modify global state. This included functions from your own code and some C library functions that are known to the compiler (strlen, strcpy, memcpy, memset, probably some of the math functions etc.).

lstrlen is not such a function, it's a OS function (in kernel32.dll) and the compiler doesn't have a clue what the function is doing, it cannot eliminate the loop becuase lstrlen my have global side effects.
Posted by Robert Wishlaw on 11/25/2012 at 8:05 PM
Hi Mike:

Thank you for the explanation of the bug. Other than strlen I wonder what other functions cause the compiler to eliminate the loop?

When the optimizer is off the problem is not there nor does it exist with lstrlen in the loop as in the example below.

The output with strlen in the first loop and lstrlen in the second is

20492338
20492338
strlen() time = 0 String length = 926
20492338
20492993
lstrlen() time = 655 String length = 926

With the optimizer (Ox) flag removed the result is

21050884
21051133
strlen() time = 249 String length = 926
21051133
21051788
lstrlen() time = 655 String length = 926

//strlenMMX7.c
#include <windows.h>
#include <stdio.h>
#include <string.h>

char a[] =
"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe:\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.\n"

"Beware the Jabberwock, my son!\n"
"The jaws that bite, the claws that catch!\n"
"Beware the Jubjub bird, and shun\n"
"The frumious Bandersnatch!\n"

"He took his vorpal sword in hand:\n"
"Long time the manxome foe he sought -\n"
"So rested he by the Tumturn tree,\n"
"And stood awhile in thought.\n"

"And, as in uffish thought he stood;\n"
"The Jabberwock, with eyes of flame,\n"
"Came whiffling through the tulgey wood,\n"
"And burbled as it came!\n"

"One, two! One, two! And through and through\n"
"The vorpal blade went snicker-snackl\n"
"He left it dead, and with its head\n"
"He went galumphing back.\n"

"And hast thou slain the Jabberwock?\n"
"Come to my arms, my beamish boy!\n"
"O frabjous day! Callooh! Callay!\n"
"He chortled in his joy.\n"

"'Twas brillig, and the slithy toves\n"
"Did gyre and gimble in the wabe:\n"
"All mimsy were the borogoves,\n"
"And the mome raths outgrabe.";

static int     x;
static int     t;
static int     tt;
static int     kk;

int main(int argc, char *argv[])
{
tt= GetTickCount();
printf("% d\n",tt);
for(x=1; x<=1000000; x+=1)
{
    kk= strlen( a);
}

t= GetTickCount();
printf("% d\n",t);
t-= tt;
printf("%s% d%s% d\n","strlen() time = ",t," String length = ",kk);

tt= GetTickCount();
printf("% d\n",tt);
for(x=1; x<=1000000; x+=1)
{
    kk= lstrlen( a);
}

t= GetTickCount();
printf("% d\n",t);
t-= tt;
printf("%s% d%s% d\n","lstrlen() time = ",t," String length = ",kk);
return 0; /* End of main program */
}



Posted by Mike Danes on 11/25/2012 at 2:07 AM
GetTickCount certainly advances but the code is too fast for GetTickCount's resolution. The compiler eliminates the for loop and replaces it with:
kk = strlen(a);
x = 1000001;
Posted by Microsoft on 11/24/2012 at 11:52 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
File Name Submitted By Submitted On File Size  
STRLENMMX6.cpp 11/24/2012 1 KB