Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:25+00:00 2026-05-30T18:20:25+00:00

I ported one project from Visual C++ 6.0 to VS 2010 and found that

  • 0

I ported one project from Visual C++ 6.0 to VS 2010 and found that a critical part of the code (scripting engine) now runs in about three times slower than in was before.
After some research I managed to extract code fragment which seems to cause the slowdown. I minimized it as much as possible, so it ill be easier to reproduce the problem.
The problem is reproduced when assigning a complex class (Variant) which contains another class (String), and the union of several other fields of simple types.

Playing with the example I discovered more “magic”:
1. If I comment one of unused (!) class members, the speed increases, and the code finally runs faster than those complied with VS 6.2
2. The same is true if I remove the “union” wrapper”
3. The same is true event if change the value of the filed from 1 to 0

I have no idea what the hell is going on.
I have checked all code generation and optimization switches, but without any success.

The code sample is below:
On my Intel 2.53 GHz CPU this test, compiled under VS 6.2 runs 1.0 second.
Compiled under VS 2010 – 40 seconds
Compiled under VS 2010 with “magic” lines commented – 0.3 seconds.

The problem is reproduces with any optimization switch, but the “Whole program optimization” (/GL) should be disabled. Otherwise this too smart optimizer will know that out test actually does nothing, and the test will run 0 seconds.

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

class String
{
public:
    char    *ptr;
    int     size;

    String() : ptr(NULL), size( 0 ) {};
    ~String() {if ( ptr != NULL ) free( ptr );};
    String& operator=( const String& str2 );
};

String& String::operator=( const String& string2 )
{
    if ( string2.ptr != NULL )
    {
        // This part is never called in our test:
        ptr = (char *)realloc( ptr, string2.size + 1 );
        size = string2.size;
        memcpy( ptr, string2.ptr, size + 1 );
    }
    else if ( ptr != NULL )
    {
        // This part is never called in our test:
        free( ptr );
        ptr = NULL;
        size = 0;
    }

    return *this;
}


struct Date
{
    unsigned short          year;
    unsigned char           month;
    unsigned char           day;
    unsigned char           hour;
    unsigned char           minute;
    unsigned char           second;
    unsigned char           dayOfWeek;
};


class Variant
{
public:
    int             dataType;
    String          valStr; // If we comment this string, the speed is OK!

    // if we drop the 'union' wrapper, the speed is OK!
    union
    {
        __int64     valInteger;

        // if we comment any of these fields, unused in out test, the speed is OK!
        double      valReal;
        bool        valBool;
        Date        valDate;
        void        *valObject;
    };

    Variant() : dataType( 0 ) {};
};


void TestSpeed()
{
    __int64             index;
    Variant             tempVal, tempVal2;

    tempVal.dataType = 3;
    tempVal.valInteger = 1; // If we comment this string, the speed is OK!

    for ( index = 0; index < 200000000; index++ )
    {
        tempVal2 = tempVal;
    }
}

int main(int argc, char* argv[])
{
    int         ticks;
    char        str[64];

    ticks = GetTickCount();

    TestSpeed();

    sprintf( str, "%.*f", 1, (double)( GetTickCount() - ticks ) / 1000 );

    MessageBox( NULL, str, "", 0 );

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:20:27+00:00Added an answer on May 30, 2026 at 6:20 pm

    This was rather interesting. First I was unable to reproduce the slow down in release build, only in debug build. Then I turned off SSE2 optimizations and got the same ~40s run time.

    The problem seems to be in the compiler generated copy assignment for Variant. Without SSE2 it actually does a floating point copy with fld/fstp instructions because the union contains a double. And with some specific values this apparently is a really expensive operation. The 64-bit integer value 1 maps to 4.940656458412e-324#DEN which is a denormalized number and I believe this causes problems. When you leave tempVal.valInteger uninitialized it may contain a value that works faster.

    I did a small test to confirm this:

    union {
        uint64_t i;
        volatile double d1;
    };
    i = 0xcccccccccccccccc; //with this value the test takes 0.07 seconds
    //i = 1; //change to 1 and now the test takes 36 seconds
    volatile double d2;
    
    for(int i=0; i<200000000; ++i)
        d2 = d1;
    

    So what you could do is define your own copy assignment for Variant that just does a simple memcpy of the union.

    Variant& operator=(const Variant& rhs)
    {
        dataType = rhs.dataType;
        union UnionType
        {
            __int64     valInteger;
            double      valReal;
            bool        valBool;
            Date        valDate;
            void        *valObject;
        };
        memcpy(&valInteger, &rhs.valInteger, sizeof(UnionType));
        valStr = rhs.valStr;
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am re-engineering a windows application to be ported to web. One area that
I have ported some LGPL code from Java to C#, which I plan to
I need to get VS 2010 projects that are using Visual Studio Tests building
Premise : The requirements for an upcoming project include the fact that no one
Members at work have come across a C# project posted on Google code that
I have a C# solution composed of three projects in Visual Sudio 2010. One
A user has sent me some information that they posted to one of my
I have just ported several of our home-made Outlook COM-addins from Delphi 2007 to
I have taken over a medium sized project that was written originally using RoR.
From what I understand of subversion if you have a repo that contains multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.