Thread: Assignment position dramatically affecting performance

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Assignment position dramatically affecting performance

    Say I have the code below:
    Code:
    #include <dbi.h>
    
    using std::cout;
    
    int main()
    {
    	clock_t t_start = clock();
    	clock_t t_fin;
    
    
    	DBI *dbh = new DBI;        // position 1
    	cout<< "Content-type: text/html\n\n";
    
    	for( long i=0; i<1000000000; i++ )
    		;
        cout<< "Counted to a billion!<br />";
    
    	//DBI *dbh = new DBI;        // position 2
    
    	char *env_qs = getenv("QUERY_STRING");
    	if( *env_qs == NULL )
    		cout<< "exe failed it's purpose<br />";
    	else
    		cout<< "You said <span style=\"font-family:sans-serif;\">\""
    	<< env_qs << "\"</span> to me<br />";
    
    	t_fin = clock();
    	cout<< "code took about " << (double)(t_fin - t_start) / CLOCKS_PER_SEC << " secs";
    
    	delete dbh;
    	return 0;
    }
    Note that DBI *dbh isn't used. That should be irrelevant, since its constructor doesn't care.

    The app usually takes ~2.5-2.8 secs to complete when the commented line is in position 2, but ~3.75-4 secs in position 1. I am utterly baffled that there is a difference, let alone the (relatively) huge discrepancy that comes up here. I've made sure the difference is consistent, and not just me but I haven't a clue as to explaining it. Any suggestions?
    Last edited by drrngrvy; 10-15-2005 at 05:04 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which OS?
    Which compiler?
    What's in a DBI?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Duh, stupid me. I'm using MSVC on Windows. DBI just has some database wrappers. Since it doesn't do anything unless one of its functions are implicitly called I can't see how the placing should matter. The only relevant class functions are:

    Code:
    DBI::DBI()
    {
    	//mysql_library_init();
    	if( !mysql_init(&mysql) ) this->fail( FAILED_ON_INIT );
    	if( !mysql_real_connect( &mysql, "localhost", NULL, NULL, "one", 0, NULL, 0 ) )
    		this->fail( FAILED_ON_CONNECT );
    }
    
    DBI::~DBI()
    {
    	mysql_close(&mysql);
    }
    I've managed to do essentially the same thing with another class too though, so I'm guessing it has something to do with c++ perhaps?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    The app usually takes ~2.5-2.8 secs to complete when the commented line is in position 2, but ~3.75-4 secs in position 1. I am utterly baffled that there is a difference, let alone the (relatively) huge discrepancy that comes up here. I've made sure the difference is consistent, and not just me but I haven't a clue as to explaining it. Any suggestions?
    Database initializatiion could take relevatively a long time; it's interprocess. And when you run the process intensive loop part first, you might somehow increase or decrease the OS priority given to the your running process, affecting the abliity of the database to initialize. Also, clock only measures CPU time.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Duh, stupid me. I'm using MSVC on Windows.
    There are many versions of both.

    > for( long i=0; i<1000000000; i++ )
    Here's another scenario. You're running on a laptop and it's detected that your program needs lots of CPU time, so it's switched on an extra fan and boosted the CPU speed.
    What sort of machine are you running this on - perhaps it as similar behaviour.

    > I've managed to do essentially the same thing with another class too though
    Was that an empty class, or a class everyone else can easily implement for testing with?

    If you want a really good win32 clock, then use this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM