strsafe functions performance

drjackool 956 Reputation points
2021-08-28T08:39:55.65+00:00

hi
strsafe.h functions do more checks to do string operations so I think standard string functions is faster is this correct? or should I use strsafe functions?
thanks

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,486 questions
{count} votes

1 answer

Sort by: Most helpful
  1. drjackool 956 Reputation points
    2021-08-28T16:03:51.49+00:00

    I used following code to measure speed and I post result:

    TCHAR buffer[1024];  
    LPCTSTR pszText = _T("This is sample text. This is sample text. This is sample text. This is sample text. This is sample text.");  
    
    LARGE_INTEGER start[4] = { 0 };  
    LARGE_INTEGER end[4] = { 0 };  
    
    QueryPerformanceCounter(&start[0]);		  
    for (UINT i = 0; i < 10000; i++)  
    {  
    	_tcscpy(buffer, pszText);  
    }	  
    QueryPerformanceCounter(&end[0]);  
    
    
    QueryPerformanceCounter(&start[1]);  
    for (UINT i = 0; i < 10000; i++)  
    {  
    	_tcscpy_s(buffer, 1024, pszText);  
    }  
    QueryPerformanceCounter(&end[1]);  
    
    QueryPerformanceCounter(&start[2]);  
    for (UINT i = 0; i < 10000; i++)  
    {  
    	lstrcpy(buffer, pszText);  
    }  
    QueryPerformanceCounter(&end[2]);  
    
    QueryPerformanceCounter(&start[3]);  
    for (UINT i = 0; i < 10000; i++)  
    {  
    	StringCchCopy(buffer, 1024, pszText);  
    }  
    QueryPerformanceCounter(&end[3]);  
    
    CString str;  
    str.Format(_T("_tcscpy(): %I64u\n_tcscpy_s(): %I64u\nlstrcpy(): %I64u\nStringCchCopy(): %I64u"),   
    	end[0].QuadPart - start[0].QuadPart,  
    	end[1].QuadPart - start[1].QuadPart,  
    	end[2].QuadPart - start[2].QuadPart,  
    	end[3].QuadPart - start[3].QuadPart);  
    MessageBox(0, str, _T("Result") , MB_OK | MB_ICONINFORMATION);  
    

    Results: (in 4)
    127287-untitled.png

    tcscpy() has best speed and StringCchCopy() has worste