Sunday, February 12, 2012

Most of the times developers are believe that sprintf is the most performance method for string copy. But I did some small test and result as fallow.

Environment : windows 7
IDE : visual studio 2010
Programing language: Visual C++

Method: Get the current system time. Copied the 100 length strings to specific char buffer 100,000,000 times continually. Get the current system time and check the time different.


Result: following table showing the result time for each function . So as conclusion strcpy is the most fastest  string copy function.  


Test
sprintf
strcpy
strncpy
strcpy_s
Test - 1
0 : 26 : 016
0 : 0 : 261
0 : 0 : 271
0 : 2 : 563
Test - 2
0 : 25 : 874
0 : 0 : 260
0 : 0 : 270
0 : 2 : 529
Test - 3
0 : 25 : 909
0 : 0 : 260
0 : 0 : 270
0 : 2 : 532
Test - 4
0 : 25 : 905
0 : 0 : 260
0 : 0 : 270
0 : 2 : 548
Test - 5
0 : 26 : 017
0 : 0 : 260
0 : 0 : 269
0 : 2 : 531
Test - 6
0 : 25 : 917
0 : 0 : 260
0 : 0 : 270
0 : 2 : 547
Test - 7
0 : 25 : 930
0 : 0 : 260
0 : 0 : 271
0 : 2 : 533
Test - 8
0 : 25 : 914
0 : 0 : 261
0 : 0 : 269
0 : 2 : 535
Test - 9
0 : 25 : 879
0 : 0 : 261
0 : 0 : 272
0 : 2 : 533
Test - 10
0 : 25 : 898
0 : 0 : 260
0 : 0 : 271
0 : 2 : 524


#include <windows.h>
#include <stdio.h>
#include <time.h>
void getDef(SYSTEMTIME t1, SYSTEMTIME t2, char name[]){

 SYSTEMTIME temp;

 if(t1.wMilliseconds < t2.wMilliseconds)
 {
  temp.wMilliseconds = t2.wMilliseconds - t1.wMilliseconds;
 }else if(t1.wMilliseconds > t2.wMilliseconds)
 {
  temp.wMilliseconds = t2.wMilliseconds - t1.wMilliseconds + 1000 ;
  if(t2.wSecond == 0)
  {
   t2.wMinute = t2.wMinute - 1;
   t2.wSecond = 59;
  }else
  {  
   t2.wSecond = t2.wSecond - 1;
  }    }else
  {
   temp.wMilliseconds = 0;
  }    if(t1.wSecond < t2.wSecond)
  {
   temp.wSecond = t2.wSecond - t1.wSecond;
  }else if(t1.wSecond > t2.wSecond)
  {  
   temp.wSecond = t2.wSecond - t1.wSecond + 60;
   t2.wMinute = t2.wMinute - 1;
  }else{    
   temp.wSecond = 0;
  }    if(t1.wMinute < t2.wMinute)
  {
   temp.wMinute = t2.wMinute - t1.wMinute;
  }    else if(t1.wMinute == t2.wMinute)
  {    
   temp.wMinute = 0;
  }    printf("Def %s %d : %d : %d\n",name,        temp.wMinute,temp.wSecond,temp.wMilliseconds);

}
void main(){

 char buffer [105];
 SYSTEMTIME st,st2;

 for (int j = 1; j < 11; j++)
 {
  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   sprintf (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }      
  GetLocalTime(&st2);
  getDef(st,st2, "sprintf");

  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strcpy  (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }  
  GetLocalTime(&st2);
  getDef(st,st2, "strcpy");

  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strncpy  (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.",100);
  }
  GetLocalTime(&st2);
  getDef(st,st2,"strncpy");


  GetLocalTime(&st);
  for(int i=0; i< 10000000; i++)
  {
   strcpy_s (buffer, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.");
  }
  GetLocalTime(&st2);
  getDef(st,st2,"strcpy_s");
 }

 scanf ("%s",buffer);
}