performance VC++ 2005 vs. VC++ 2003

The code below runs in 17 seconds on my machine when compiled under Release mode on VC++ 2005 and in 13 seconds on VC++ 2003. This is an extract of the code so don't worry it does not make much sense.

First I though it was because of checked iterators but it does not make a difference if I use #define _SECURE_SCL 0 or not. What else could it be

Thanks,

Bumbrlik

--------------------------

// #define _SECURE_SCL 0

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include <time.h>

using namespace std ;

string ReadString(istream &in)

{

string token;

const char separator = ',';

char ch;

while(in.get(ch))

{

if(ch == '\n')

{

return token;

}

else if(ch == separator)

return token;

else if(ch != separator)

token += ch;

}

return token;

}

void main()

{

time_t start, finish;

time( &start );

string historyFileName = "c:\\Data\\AdjustedCleanSP500.csv";

cout << "Reading " << historyFileName << endl;

ifstream historyFile;

for(unsigned int run=0; run<5; ++run)

{

cout << run << ",";

historyFile.open(historyFileName.c_str());

if(!historyFile.is_open())

{

cout << "::the file cannot be opened." << endl;

exit(0);

}

unsigned int assetCount = 500;

char line[30000];

historyFile.getline(line, 30000);

while(!historyFile.eof())

{

string date = ReadString(historyFile);

// sometimes there is an extra line (for example from Excel) and eof() doesn't catch it

if(date == "")

break;

vector<double> priceSnapshot(assetCount, 0.0);

for(unsigned int asset=0; asset<assetCount; ++asset)

{

string stringValue = ReadString(historyFile);

if(!stringValue.empty())

priceSnapshot[asset] = atof(stringValue.c_str());

}

}

historyFile.close();

historyFile.clear();

}

time( &finish );

double elapsed_time = difftime( finish, start );

cout << "done in " << elapsed_time << endl;

}

------

sample file:

"Date","A","AA","AAPL","ABC","ABI","ABK","ABS","ABT","ACE","ACS","ACV","ADBE","ADCT","ADI","ADM","ADP","ADSK","AEE","AEP","AES","AET","AFL","AGN","AHC","AIG","AIV","ALL","ALTR","AMAT","AMCC","AMD","AMGN","AMP","AN","ANDW","AOC","APA","APC","APCC","APD","APOL","ASD","ASH","ASN","ASO","AT","ATI","AV","AVP","AVY","AW","AXP","AYE","AZO","BA","BAC","BAX","BBBY","BBT","BBY","BC","BCR","BDK","BDX","BEN","BF","BHI","BIIB","BJS","BK","BLI","BLL","BLS","BMC","BMET","BMS","BMY","BNI","BOL","BR","BRCM","BSC","BSX","BUD","C","CA","CAG","CAH","CAT","CB","CBE","CBSS","CC","CCE","CCL","CCU","CD","CEG","CFC","CHIR","CI","CIEN","CIN","CINF","CIT","CL","CLX","CMA","CMCSA","CMI","CMS","CMVT","CMX","CNP","COF","COH","COL","COP","COST","CPB","CPN","CPWR","CSC","CSCO","CSX","CTAS","CTB","CTL","CTX","CTXS","CVG","CVH","CVS","CVX","CZN","D","DCN","DD","DDS","DE","DELL","DG","DGX","DHI","DHR","DIS","DJ","DOV","DOW","DRI","DTE","DUK","DVN","DYN","EBAY","EC","ECL","ED","EDS","EFX","EIX","EK","EMC","EMN","EMR","EOG","EOP","EP","EQR","ERTS","ESRX","ET","ETN","ETR","EXC","F","FCX","FD","FDC","FDO","FDX","FE","FHN","FII","FISV","FITB","FLR","FNM","FO","FPL","FRE","FRX","FSH","FSL","GAS","GCI","GD","GDT","GDW","GE","GENZ","GILD","GIS","GLW","GM","GP","GPC","GPS","GR","GS","GT","GTW","GWW","HAL","HAS","HBAN","HCA","HCR","HD","HDI","HET","HIG","HLT","HMA","HNZ","HON","HOT","HPC","HPQ","HRB","HSP","HSY","HUM","IBM","IFF","IGT","INTC","INTU","IP","IPG","IR","ITT","ITW","JBL","JCI","JCP","JDSU","JNJ","JNS","JNY","JP","JPM","JWN","K","KBH","KEY","KG","KLAC","KMB","KMG","KMI","KO","KR","KRB","KRI","KSE","KSS","LEG","LEH","LEN","LH","LIZ","LLL","LLTC","LLY","LMT","LNC","LOW","LPX","LSI","LTD","LTR","LU","LUV","LXK","MAR","MAS","MAT","MBI","MCD","MCK","MCO","MDP","MDT","MEDI","MEL","MER","MERQE","MET","MHP","MHS","MI","MIL","MKC","MMC","MMM","MNST","MO","MOLX","MON","MOT","MRK","MRO","MSFT","MTB","MTG","MU","MUR","MWD","MWV","MXIM","MYG","MYL","NAV","NBR","NCC","NCR","NE","NEM","NFB","NI","NKE","NOC","NOV","NOVL","NSC","NSM","NTAP","NTRS","NUE","NVDA","NVLS","NWL","NWS","NYT","ODP","OMC","OMX","ORCL","OXY","PAYX","PBG","PBI","PCAR","PCG","PCL","PD","PDCO","PEG","PEP","PFE","PFG","PG","PGL","PGN","PGR","PH","PHM","PKI","PLD","PLL","PMCS","PMTC","PNC","PNW","PPG","PPL","PRU","PSA","PTV","PX","Q","QCOM","QLGC","R","RAI","RBK","RDC","RF","RHI","RIG","ROH","ROK","RRD","RSH","RTN","RX","S","SAFC","SANM","SBC","SBL","SBUX","SCH","SEBL","SEE","SFA","SGP","SHLD","SHW","SIAL","SLB","SLE","SLM","SLR","SNA","SNV","SO","SOV","SPG","SPLS","SRE","STA","STI","STJ","STT","STZ","SUN","SUNW","SVU","SWK","SWY","SYK","SYMC","SYY","T","TAP","TE","TEK","TER","TGT","THC","TIF","TIN","TJX","TLAB","TMK","TMO","TRB","TROW","TSG","TSN","TWX","TXN","TXT","TXU","TYC","UIS","UNH","UNM","UNP","UPS","USB","UST","UTX","UVN","VC","VFC","VIA","VLO","VMC","VNO","VZ","WAG","WAT","WB","WEN","WFC","WFT","WHR","WLP","WM","WMB","WMI","WMT","WPI","WWY","WY","WYE","X","XEL","XL","XLNX","XOM","XRX","XTO","YHOO","YUM","ZION","ZMH"
1/2/1990 0:00:00 AM,,6.72,8.7,,,,8.53,5.8,,,6.55,1.12,4.07,1.58,5.73,5.31,4.15,,12.21,,9.39,1.74,5.94,34.67,8.35,,,.45,.46,,4.12,1.07,,,2.49,7.81,6.44,15.27,.34,7.16,,,19.45,,2.58,8.06,,,1.99,11.03,,4.04,9.49,,15.58,6.69,4.02,,,.23,10.11,6.6,12.84,5.42,3.8,9.92,16.28,,,1.94,2.32,5.34,8.24,,3,4.54,7.95,7.67,19.02,16.95,,3.9,,6.87,1.74,3.38,5.98,3.55,5.22,13,17.57,1.64,3.31,4.94,3.94,,,10.41,.74,,12.53,,2.94,,,4.53,7.07,,5.19,18,22.94,,,2.1,,,,7.65,11.46,7.02,,,9.6,,12.86,,5.27,8.12,3.28,,,,4.63,9.74,,5.15,6.53,12.75,21.92,10.02,.06,.58,,,1.86,8.58,17.24,7.09,13.13,,4.25,6.87,,,,3.42,2.25,11.15,10.78,2.27,10.29,19.73,.07,,9.84,5.55,,,,,,,8.17,5.01,5.52,4.34,,,,1.13,11.89,,,,,,,4.76,2.23,8.48,3.66,2.59,,,6.08,12.56,1.3,,4.02,3.87,,,8.34,5.08,19.52,4.45,7.5,1.73,6.68,,15.64,,11.23,12.07,5.02,,,,1.74,1.12,5.34,,11.14,,9.59,6.18,12.81,8.91,4,3.06,,6.56,8.12,18.57,10.05,,1.07,,14.79,4.54,5.08,,8.45,,3.97,11.83,,5.72,,,5.68,8.99,7.93,11.31,5.75,2.82,,,8.84,20.43,6.86,7.7,3.85,,16.03,,,2.73,,1.06,,9.01,,,11.77,8.41,5.21,1.64,7.84,,4.61,23,,1.03,,,8.67,4.13,7.05,6.99,,,7.08,1.72,,1.77,1.98,,,4.83,,,9.2,,8.31,12.7,,6.92,,,3.96,8.38,8.53,.54,,,.98,3.63,,12.31,,8.83,3.17,41.25,,4.46,,,29.31,,2.39,5.26,3.52,,3.95,6.42,3.61,,,6.59,,,6.37,,8.48,,,22.71,.6,14.75,,,7.94,5.68,4.82,.61,12.52,,3.42,7.77,2.2,,6.39,10.58,9.86,3.75,6.46,1.23,4.45,,,,,6.02,5.4,9.03,4.2,,1.83,,,,,,7.68,,17.35,11.81,,,,5.98,1.74,13.14,8.44,16.09,,6.67,8.41,,5.74,2.1,,.26,,5.16,3.8,3.9,,5.3,,16.93,4.06,3.49,,9.75,,1.52,,,,,6.15,5.65,3.96,3.51,,5.86,.56,6.62,8.74,,1.51,,3.33,44.19,15.08,6.42,3.75,2.72,4.35,9.15,4.95,9.03,1.01,,7.56,7.77,9.41,2.39,,7.32,,1.8,6.24,13.77,5.89,14.64,,7.25,18.91,,2.56,4.18,4.9,,,8.28,,1.58,10.16,3.26,14.87,2.56,,9.8,3.73,3.65,,21.33,,,2.77,,5.34,,7.16,10.85,8.7,,8.16,,,7.4,6.63,,,,,
1/3/1990 0:00:00 AM,,6.82,8.76,,,,8.74,5.82,,,6.64,1.18,4.05,1.5,5.73,5.29,4.25,,12.16,,9.35,1.7,6.15,34.22,8.34,,,.46,.44,,4,1.1,,,2.54,7.79,6.53,15.11,.35,7.16,,,19.45,,2.57,8.12,,,2.01,11.03,,4.08,9.46,,15.96,6.75,4.12,,,.25,10.02,6.68,13,5.36,3.91,10.16,16.04,,,1.97,2.24,5.18,8.2,,3.05,4.45,8.11,7.88,18.77,16.83,,4.04,,6.96,1.76,3.45,6,3.59,5.28,13.05,17.57,1.61,3.27,4.9,3.89,,,10.22,.74,,12.45,,2.94,,,4.51,7.07,,5.22,17.83,23.47,,,2.1,,,,7.5,11.64,6.96,,,9.54,,12.86,,5.58,8.15,3.25,,,,4.72,9.58,,5.1,6.55,12.76,21.77,10.2,.06,.59,,,1.86,8.6,16.47,6.97,13.37,,4.23,6.88,,,,3.46,2.29,10.96,10.83,2.29,10.19,19.73,.08,,9.97,5.52,,,,,,,8.14,4.87,5.52,4.36,,,,1.13,12.04,,,,,,,4.93,2.23,8.36,3.78,2.62,,,6.2,12.63,1.33,,3.99,3.86,,,8.36,5.13,19.41,4.5,7.37,1.74,6.75,,15.85,,10.98,12,4.99,,,,1.74,1.14,5.36,,11.04,,9.32,6.2,13.52,8.88,3.92,3.03,,6.51,8.1,18.74,10.05,,1.04,,14.76,4.59,5.21,,8.25,,3.97,11.99,,5.74,,,5.58,9.29,8.08,11.16,5.8,2.82,,,8.81,20.23,6.76,7.58,3.81,,15.61,,,2.71,,1.05,,9.05,,,11.77,8.36,5.23,1.63,7.91,,4.72,23.28,,1.01,,,8.76,4,7.03,6.92,,,6.98,1.73,,1.78,1.97,,,4.8,,,9.7,,8.26,12.84,,7.03,,,4.05,8.51,8.47,.54,,,1.01,3.67,,12.56,,8.89,3.05,41.25,,4.58,,,29.08,,2.36,5.14,3.52,,4.12,6.21,3.49,,,6.69,,,6.34,,7.94,,,22.77,.53,14.75,,,7.94,5.71,4.82,.61,12.97,,3.34,7.69,2.21,,6.37,10.62,9.81,3.77,6.52,1.24,4.4,,,,,6.16,5.64,9.28,4.22,,1.85,,,,,,7.68,,17.69,11.81,,,,5.87,1.68,13.33,8.24,16.15,,6.71,8.57,,5.64,2.12,,.27,,5.19,3.88,3.94,,5.34,,16.98,4.04,3.59,,9.67,,1.53,,,,,6.19,5.62,3.85,3.55,,5.75,.56,6.45,8.41,,1.55,,3.24,43.84,15.45,6.39,3.72,2.72,4.39,9.29,5.29,8.89,1.03,,7.48,8.34,9.46,2.43,,7.35,,1.76,6.05,13.72,5.98,14.29,,7.37,18.65,,2.48,4.14,4.85,,,8.19,,1.61,10.19,3.22,14.84,2.52,,9.34,3.73,3.49,,21.42,,,2.83,,5.34,,7.06,10.61,8.75,,8.14,,,7.33,6.68,,,,,
1/4/1990 0:00:00 AM,,6.83,8.79,,,,8.76,5.81,,,6.68,1.23,3.96,1.48,5.69,5.26,4.15,,11.98,,9.37,1.66,6.15,33.59,8.22,,,.47,.44,,3.94,1.12,,,2.54,7.67,6.16,14.7,.35,7.07,,,19.33,,2.64,8.12,,,2,11.16,,4.04,9.43,,15.9,6.6,4.04,,,.25,10.02,6.64,13,5.32,4,10.2,15.65,,,1.96,2.24,5.24,8.11,,3.05,4.29,8.05,8,18.8,16.48,,4.01,,6.87,1.74,3.48,5.82,3.59,5.3,13,17.4,1.62,3.29,4.9,3.97,,,10.11,.75,,12.45,,2.9,,,4.47,7.05,,5.11,18,23.17,,,2.08,,,,7.35,11.76,6.83,,,9.37,,12.73,,5.47,7.92,3.08,,,,4.73,9.46,,4.99,6.53,12.75,21.41,10.2,.06,.58,,,1.89,8.59,16.47,6.99,13.15,,4.15,6.84,,,,3.55,2.34,10.77,10.78,2.31,10.03,19.9,.08,,9.84,5.38,,,,,,,8.03,4.87,5.43,4.36,,,,1.11,11.95,,,,,,,4.89,2.18,8.16,3.8,2.64,,,6.03,12.74,1.32,,3.9,3.84,,,8.45,5.09,19.36,4.53,7.35,1.76,6.81,,15.68,,11,11.75,5.11,,,,1.76,1.15,5.29,,10.67,,9.22,6.4,14.19,9.02,3.88,3.02,,6.47,8.03,18.95,10.11,,1.07,,14.7,4.57,5.18,,8.09,,3.94,12.18,,5.75,,,5.36,9.32,8.08,11.12,5.61,2.85,,,8.79,19.77,6.79,7.55,3.81,,15.54,,,2.71,,1.05,,8.96,,,11.71,8.33,5.25,1.64,7.89,,4.74,22.95,,1.02,,,8.8,4.07,7.03,6.79,,,6.98,1.78,,1.79,1.96,,,4.9,,,9.66,,8.18,12.94,,6.78,,,4.07,8.38,8.68,.56,,,.99,3.6,,12.61,,8.83,3.01,41.25,,4.58,,,29.23,,2.36,5.04,3.49,,4.12,6.31,3.49,,,6.68,,,6.31,,8.32,,,22.46,.55,14.69,,,7.94,5.61,4.76,.61,13.23,,3.28,7.62,2.25,,6.25,10.02,9.57,3.75,6.7,1.21,4.4,,,,,6.27,5.52,9.2,4.18,,1.85,,,,,,7.68,,17.8,11.31,,,,5.81,1.64,13.23,8.31,16.38,,6.3,8.91,,5.66,2.15,,.27,,5.25,3.66,3.93,,5.26,,16.5,3.97,3.55,,9.64,,1.46,,,,,6.31,5.62,3.77,3.52,,5.91,.55,6.56,8.41,,1.55,,3.18,43.12,15.53,6.31,3.72,2.75,4.4,9,5.23,8.88,1.05,,7.59,8.4,9.38,2.49,,7.32,,1.7,6.08,13.62,5.95,14.41,,7.48,18.56,,2.48,4.08,4.84,,,8.19,,1.63,10.14,3.2,14.4,2.44,,9.46,3.54,3.45,,21.66,,,2.75,,5.31,,6.8,10.52,8.62,,7.94,,,7.25,6.7,,,,,
1/5/1990 0:00:00 AM,,6.77,8.82,,,,8.66,5.75,,,6.73,1.26,3.99,1.48,5.45,5.22,4.15,,11.7,,9.31,1.66,6.07,33.24,7.97,,,.46,.43,,3.82,1.14,,,2.52,7.51,5.98,14.45,.33,7.16,,,18.83,,2.6,8.09,,,1.97,10.82,,4,9.35,,15.68,6.46,3.98,,,.25,10.02,6.53,12.92,5.31,4.11,10.09,15.57,,,1.97,2.32,5.28,7.85,,3.02,4.09,7.81,7.76,18.37,16.13,,4.01,,6.85,1.76,3.38,5.74,3.66,5.27,12.91,17.07,1.61,3.21,4.94,3.94,,,9.96,.77,,12.37,,2.88,,,4.32,6.93,,5,17.87,22.86,,,2.05,,,,7.28,11.83,6.62,,,9.33,,12.39,,5.47,7.89,3.2,,,,4.69,9.31,,4.91,6.62,12.64,20.89,10.04,.06,.55,,,1.86,8.62,15.83,6.97,13.08,,4.13,6.76,,,,3.66,2.4,10.67,10.78,2.29,9.81,19.73,.08,,9.78,5.3,,,,,,,8.01,4.79,5.43,4.3,,,,1.09,11.64,,,,,,,4.89,2.17,8.05,3.77,2.66,,,5.97,12.49,1.31,,3.79,3.8,,,8.33,5.08,19.09,4.53,7.37,1.75,6.77,,15.51,,11,11.71,5.18,,,,1.73,1.15,5.24,,10.46,,9.15,6.49,13.93,9.07,3.83,3,,6.42,8,18.9,10.02,,1.06,,14.73,4.6,5.17,,8.05,,3.89,12.02,,5.69,,,5.23,9.36,8.08,11.08,5.51,2.85,,,8.73,19.47,6.73,7.49,4.03,,15.3,,,2.68,,1.05,,8.87,,,11.58,8.56,5.15,1.64,7.82,,4.63,22.82,,1.01,,,8.89,4.05,7,6.67,,,6.93,1.82,,1.77,1.94,,,4.84,,,9.66,,8.06,12.72,,6.82,,,4,8.21,8.56,.55,,,.97,3.6,,12.56,,8.83,3.01,38.75,,4.58,,,30,,2.35,5.19,3.52,,4.09,6.19,3.43,,,6.63,,,6.31,,8.25,,,22.34,.52,14.62,,,7.98,5.45,4.65,.6,13.15,,3.19,7.51,2.23,,6.14,9.97,9.47,3.76,6.61,1.16,4.43,,,,,6.21,5.58,9.09,4.14,,1.83,,,,,,7.55,,17.57,11.19,,,,5.87,1.67,13.1,8.13,16.15,,6.21,8.88,,5.39,2.15,,.27,,5.19,3.7,3.88,,5.3,,16.42,4,3.51,,9.75,,1.44,,,,,6.28,5.62,3.73,3.53,,5.88,.55,6.51,8.3,,1.57,,3.18,42.41,14.98,6.15,3.9,2.66,4.41,8.8,5.22,8.93,1.04,,7.41,8.34,9.11,2.47,,7.35,,1.68,6.21,13.62,5.98,14.76,,7.58,18.56,,2.43,4.08,4.84,,,8.25,,1.65,10.16,3.2,14.07,2.38,,9.62,3.63,3.35,,21,,,2.75,,5.25,,6.8,10.47,8.53,,7.86,,,7.21,6.71,,,,,



Answer this question

performance VC++ 2005 vs. VC++ 2003

  • lori1171963

    You are right that the difference is in single vs. multi-threaded. Also, the slowdown is in the .get() function.

    The flollowing code (when I supply a big file)

    char ch;

    while(!historyFile.eof()) historyFile.get(ch);

    runs in

    10 seconds on 2005 (/MT)

    4 seconds on 2003 (/ML)

    11 seconds on 2003 (/MT)

    I understand the multi-threaded overhead but darn, I wish I could somehow turn it down.

    Bumbrlik


  • buzzlitebeer

    I also feel like it's worth mentioning that if you are sure your program is single-threaded for IO, you can use the _nolock (e.g. _fread_nolock) family of IO functions that avoid the locking.

    But as others have noticed, it is probably better to refactor your code not to do IO in single character chunks.

    Martyn


  • JuanCarlosUlti

    Could this be related to http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=131346&SiteID=1

    Also, are you using the /fp:fast switch

    If your are still seeing such loss in perf with such switches then please include all compiler switches used in both cases so that folks on the forum can reproduce the issue.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • M4tt

    Thanks for verifying!

    I just sent an email to other folks and pointed them to the link. They should be looking into it soon. I will keep you updated.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Jorn Andre

    In the 2003 case, you are using single threaded runtime library.  In the 2005 case you are using multi threaded library (since single threaded no longer exists).

    I created a 4 meg file based on the data you provided, and here are the results:

    2003:

    Single threaded: 7 seconds

    Multi threaded: 11 seconds

    2005:

    Multi threaded: 9 seconds.

    I profiled these apps with Rational, and Enter/LeaveCriticalSection are a major portion of the time spent (except in the single threaded 2003 case, where this performance hit is non-existent)

    There is no possible way a multi-threaded approach will match the performance of the single threaded approach, due to the amount of locking/unlocking, TLS values, etc that come into play.  By abandoning the single threaded library this is the result that can occur in some situations, unfortunately.   In most real-world cases you should be ok.

    Some stats for my 4 meg test file when running in 2005:

    22 million calls to EnterCriticalSection

    22 million calls to LeaveCriticalSection (about 15% of the overall time in the process)

    11.8 million calls to TlsGetValue (this can be reduced to 7.9 million by using the tidtable optimization at http://www.codeproject.com/cpp/improved2005crt.asp )


  • DragonWalsh

    Not to detract from your wish for "the way it used to be," but you could easily rewrite this.  Do an fread() with a large buffer (8k, perhaps), repeat until fread returns zero, and you'll practically remove all overhead of the CRT (and then you're up against the I/O bottleneck.)

     


  • Moumst

    Thanks for your answer.

    First, I simplified the code and I am not using any vectors anymore (see below the main loop ). Now the difference in performance is even more significant, 7 sec on VC++ 2005 vs. 4 sec on VC++ 2003.

    I turned on /fp:fast but obviously now it won't make any difference now. I also changed /MD to /MT but it did not help.

    VC++ 2003 switches:

    /O2 /GA /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /EHsc /ML /GS /Zc:forScope /GR /Fo"Release/" /Fd"Release/vc70.pdb" /W3 /nologo /c /Wp64 /Zi /TP /wd4267 /wd4355

    VC++ 2005 switches:

    /O2 /Ot /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /GF /FD /EHsc /MT /fp:fast /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Wp64 /Zi /TP /errorReport:prompt

    ------------------------

    while(!historyFile.eof())

    {

    if(ReadString(historyFile) == "")

    break;

    for(unsigned int asset=0; asset<assetCount; ++asset)

    ReadString(historyFile);

    }


  • performance VC++ 2005 vs. VC++ 2003