[ create a new paste ] login | about

Link: http://codepad.org/iwWrAoyK    [ raw code | output | fork ]

C++, pasted on Aug 22:
#include <stdio.h>

#include <map>

#include <sys/time.h>
#include <unistd.h>

//https://kldp.org/node/101730
//https://www.joinc.co.kr/w/man/2/gettimeofday

using namespace std;

//
class people
{
public:
    int number;
    int grade;
};

//
struct timeval val_old;
void getdoubledt(char *dt)
{
  struct timeval val;
  struct tm *ptm;
 
  gettimeofday(&val, NULL);
  ptm = localtime(&val.tv_sec);
 
  memset(dt , 0x00 , sizeof(dt));
 
  // format : YYMMDDhhmmssuuuuuu
  sprintf(dt, "%04d% 02d %02d %02d %02d %02d %06ld"
      , ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday
      , ptm->tm_hour, ptm->tm_min, ptm->tm_sec
      , val.tv_usec);

  printf("%06ld\t", val.tv_usec - val_old.tv_usec);

  val_old = val;

}

int main()
{
    int i = 0;
    char date[1024];

    getdoubledt(&date[0]);    printf("시작 : \t\t%s\n", date);
  

    //1000 개는 Segment Fault
    int num_of_people = 100;
    vector<people> *Ptr2 = new vector<people>(num_of_people);
 
    for(int i=0; i!=num_of_people; ++i)
    {
        (*(people*)&Ptr2[i]).number = i;
	(*(people*)&Ptr2[i]).grade = i * 10;
    }
    getdoubledt(&date[0]);    printf("vector : \t%s\n", date);

    for(int i=0; i!=num_of_people; ++i)
    {
        if(i % 10 == 0)
        {
            printf("Index : %d\n", i);
            printf("Current number : %d\n", (*(people*)&Ptr2[i]).number);
            printf("Current grade : %d\n", (*(people*)&Ptr2[i]).grade);
        }
    }
    delete Ptr2 ;
    //memory clobbered past end of allocated block
    //Exited: ExitFailure 127

    //Segmentation fault
    getdoubledt(&date[0]);    printf("vector pop : \t%s\n", date);

    return 0;
}


#if 0
706828	시작 : 		2018 8 22 02 06 06 706828
000680	a = b + c : 	2018 8 22 02 06 06 707508
000349	strcpy(f, d) : 	2018 8 22 02 06 06 707857
000160	memcpy(f, d) : 	2018 8 22 02 06 06 708017
004131	sprintf() : 	2018 8 22 02 06 06 712148
000224	f[] = e[] : 	2018 8 22 02 06 06 712372
000140	g[] = &e[] : 	2018 8 22 02 06 06 712512
001118	map[] = 0x00 : 	2018 8 22 02 06 06 713630
000924	map[] = b : 	2018 8 22 02 06 06 714554
000180	z[0] : 		2018 8 22 02 06 06 714734

sprintf       느리다. 4000
map           느리다. 1000
+ 연산 더하기 보통.   300
memcpy        빠르다. 150

for문을 개선할 필요가 있다.
함수 보다 낱개로 하는 작은 연산이 더 빠르다.
기능 하나당. 시간이 추가 된다.
#endif


Output:
1
2
3
memory clobbered past end of allocated block

Exited: ExitFailure 127


Create a new paste based on this one


Comments: