Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. weird optimization

weird optimization

Scheduled Pinned Locked Moved Clever Code
databasealgorithmsdata-structuresperformancehelp
19 Posts 9 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mladen Jankovic

    This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way) int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; } But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused: volatile bool duplicate = false; or for( volatile int j = 0; j < i; j++ )

    Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

    R Offline
    R Offline
    Robert Surtees
    wrote on last edited by
    #7

    I don't see a srand() to seed rand(). I think the default is srand( 1 ) if none is given but that might not be the case in your implementation.

    1 Reply Last reply
    0
    • M Mladen Jankovic

      This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way) int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; } But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused: volatile bool duplicate = false; or for( volatile int j = 0; j < i; j++ )

      Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #8

      Congratulations, you've found an optimization bug. Please report this to your compiler vendor. I'm guessing it's erroneously reusing a register, or not saving a value back to a memory location, or something like that. volatile requires the compiler to go back to the memory location every time (because it could have changed in a different code path) rather than use a previous value stored in a register.

      Stability. What an interesting concept. -- Chris Maunder

      M 1 Reply Last reply
      0
      • P PIEBALDconsult

        Along with what compiler was used. -- modified at 16:30 Wednesday 8th August, 2007 I've been unable to reproduce it with Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland

        M Offline
        M Offline
        Mladen Jankovic
        wrote on last edited by
        #9

        Posted code was just part of a method, and originally it's not usin built-in random generator. I have made test project, to see what's wrong.

        int _tmain(int argc, _TCHAR* argv[])
        {
        srand( (unsigned int) time(NULL) );

        int size = 10;
        int\* old = new int\[ size \];
        
        for( int i = 0; i < size; i++ )
        {
        	int index;
        	bool duplicate = false;
        	do	
        	{		
        		index = rand() % size;
        		for( int j = 0; j < i; j++ )
        		{			
        			duplicate = old\[ j \] == index;			
        			if( duplicate )				
        				break;		
        		}	
        	} while( duplicate );	
        
        	old\[ i \] = index;
        }
        
        for( int i = 0; i < size; i++ )
        	cout << old\[ i \] << "\\n";
        
        for( int i = 0; i < size; i++ )
        {
        	for( int j = i + 1; j < size; j++ )
        	{
        		if( old\[ i \] == old\[ j \] )
        			cout << "old\[" << i << "\]==old\[" << j << "\]==" << old\[ i \] << "\\n";
        	}
        }
        
        delete old;
        
        return 0;
        

        }

        Ouput: 8 3 7 6 0 9 8 3 1 4 old[0]==old[6]==8 old[1]==old[7]==3 Compiler used is MS VC++ 8.0. Building project as Debug produces no duplicates, but as Release produces more the one duplicate.

        Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

        P 1 Reply Last reply
        0
        • M Mike Dimmick

          Congratulations, you've found an optimization bug. Please report this to your compiler vendor. I'm guessing it's erroneously reusing a register, or not saving a value back to a memory location, or something like that. volatile requires the compiler to go back to the memory location every time (because it could have changed in a different code path) rather than use a previous value stored in a register.

          Stability. What an interesting concept. -- Chris Maunder

          M Offline
          M Offline
          Mladen Jankovic
          wrote on last edited by
          #10

          And what if I don't? :D By the way, I'm I the only one who think this message board (last two threads) are messed up?

          Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

          P 1 Reply Last reply
          0
          • M Mladen Jankovic

            Posted code was just part of a method, and originally it's not usin built-in random generator. I have made test project, to see what's wrong.

            int _tmain(int argc, _TCHAR* argv[])
            {
            srand( (unsigned int) time(NULL) );

            int size = 10;
            int\* old = new int\[ size \];
            
            for( int i = 0; i < size; i++ )
            {
            	int index;
            	bool duplicate = false;
            	do	
            	{		
            		index = rand() % size;
            		for( int j = 0; j < i; j++ )
            		{			
            			duplicate = old\[ j \] == index;			
            			if( duplicate )				
            				break;		
            		}	
            	} while( duplicate );	
            
            	old\[ i \] = index;
            }
            
            for( int i = 0; i < size; i++ )
            	cout << old\[ i \] << "\\n";
            
            for( int i = 0; i < size; i++ )
            {
            	for( int j = i + 1; j < size; j++ )
            	{
            		if( old\[ i \] == old\[ j \] )
            			cout << "old\[" << i << "\]==old\[" << j << "\]==" << old\[ i \] << "\\n";
            	}
            }
            
            delete old;
            
            return 0;
            

            }

            Ouput: 8 3 7 6 0 9 8 3 1 4 old[0]==old[6]==8 old[1]==old[7]==3 Compiler used is MS VC++ 8.0. Building project as Debug produces no duplicates, but as Release produces more the one duplicate.

            Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #11

            Mladen Jankovic wrote:

            Compiler used is MS VC++ 8.0

            Oh, well, there's your problem. :-D Could you try my version and let me know whether or not it works?

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              Mladen Jankovic wrote:

              Compiler used is MS VC++ 8.0

              Oh, well, there's your problem. :-D Could you try my version and let me know whether or not it works?

              M Offline
              M Offline
              Mladen Jankovic
              wrote on last edited by
              #12

              PIEBALDconsult wrote:

              Could you try my version and let me know whether or not it works?

              Yes it works.

              Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

              1 Reply Last reply
              0
              • M Mladen Jankovic

                And what if I don't? :D By the way, I'm I the only one who think this message board (last two threads) are messed up?

                Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #13

                Just out of curiosity, which compiler are you using?


                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                My first real C# project | Linkify!|FoldWithUs! | sighist

                M 1 Reply Last reply
                0
                • P peterchen

                  Just out of curiosity, which compiler are you using?


                  We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                  My first real C# project | Linkify!|FoldWithUs! | sighist

                  M Offline
                  M Offline
                  Mladen Jankovic
                  wrote on last edited by
                  #14

                  VC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132

                  Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                  P M 2 Replies Last reply
                  0
                  • M Mladen Jankovic

                    VC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132

                    Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                    P Offline
                    P Offline
                    peterchen
                    wrote on last edited by
                    #15

                    That's freaking scary!


                    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                    My first real C# project | Linkify!|FoldWithUs! | sighist

                    M 1 Reply Last reply
                    0
                    • P peterchen

                      That's freaking scary!


                      We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                      My first real C# project | Linkify!|FoldWithUs! | sighist

                      M Offline
                      M Offline
                      Mladen Jankovic
                      wrote on last edited by
                      #16

                      Bug or what? ;)

                      Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                      1 Reply Last reply
                      0
                      • M Mladen Jankovic

                        This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way) int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; } But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused: volatile bool duplicate = false; or for( volatile int j = 0; j < i; j++ )

                        Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #17

                        Your code snippet seems a bit inefficient. Try something like:

                        __inline void Swap( int &a, int &b )
                        {
                        int t = b;
                        b = a;
                        a = t;
                        }

                        void main( void )
                        {
                        int array[10] = {0};
                        int n = 10;

                        for (int i = 0; i < n; i++)
                            array\[i\] = i;
                        
                        for (i = 0; i < n; i++)
                        { 
                            int j = rand() % n;
                            Swap(array\[j\], array\[i\]);
                        }
                        

                        }


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        1 Reply Last reply
                        0
                        • M Mladen Jankovic

                          This peace of code (which should generate array of unique random numbers) works perfectly with on optimization. (No multithreading, by the way) int* old = new int[ size ]; for( int i = 0; i < size; i++ ) { int index; bool duplicate = false; do { index = rand(); for( int j = 0; j < i; j++ ) { duplicate = old[ j ] == index; if( duplicate ) break; } } while( duplicate ); old[ i ] = index; } But if I turn on optimization sometimes it produce exactly on duplicate. Adding volatile fixs the problem, but still i don't understand :confused: volatile bool duplicate = false; or for( volatile int j = 0; j < i; j++ )

                          Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                          G Offline
                          G Offline
                          ghle
                          wrote on last edited by
                          #18

                          Good programmers don't "break" out of "for" loops.

                          1 Reply Last reply
                          0
                          • M Mladen Jankovic

                            VC++ 8 SP1, but it looks like that Beta version of Orcas has same bug. I have posted message on MSDN forum, and their moderator submited bug to VS team: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?feedbackID=292132

                            Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

                            M Offline
                            M Offline
                            Member 1229083
                            wrote on last edited by
                            #19

                            Well done, good catch! "Codegen problem with nested do-while / for loops (/O2)" I just gave my vote of Very-Important. Note-to-self: try to avoid flying with Microsoft powered airplanes.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups