- ÂÛ̳»ÕÕÂ:
- 0
|
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#define NUM 350000
struct timeval tv1,tv2;
struct timezone tz1,tz2;
long int starttime,endtime;
int a[NUM],b[NUM];
void one_ms();
void one_ms()
{
for(int i=0;i<NUM;i++)
{
a[i]+=b[i];
}
}
int main()
{
for(int i=0;i<NUM;i++)
{
a[i]=rand()%9;
b[i]=rand()%9;
}
gettimeofday(&tv1, &tz1);
one_ms();
gettimeofday(&tv2, &tz2);
starttime=tv1.tv_sec*1000000+tv1.tv_usec;
endtime=tv2.tv_sec*1000000+tv2.tv_usec;
//printf("%d\t",j );
printf("start time is %ld\t",starttime);
printf("end time is %ld\t",endtime);
printf("time slaped: %ld(us)\n", (endtime-starttime));
return 0;
}
ÉÏÃæµÄone_ms´ó¸Å1ms£¬ÎªÊ²Ã´ÔÚÏÂÃæ³ÌÐòÖÐÔËÐÐʱ±ä³É2ms
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM 352000
#define CPU_NUM 3
using namespace std;
void one_ms();
#include "/root/×ÀÃæ/test_sem/interface/test_sem_interface.h"
#include <pthread.h>
FILE* fLog = NULL;
int a[NUM],b[NUM];
void one_ms()
{
for(int i=0;i<NUM;i++)
{
a[i]+=b[i];
}
}
int main()
{
for(int i=0;i<NUM;i++)
{
a[i]=rand()%9;
b[i]=rand()%9;
}
char pth[256];
sprintf(pth,"/root/×ÀÃæ/test_sem/test_sem_wait/result/%d.txt",NUM);
fLog = fopen(pth, "a");
key_t key = ftok(TEST_SHM_NAME, 'R');
int shmid = shmget(key, TEST_SHM_SIZE, 0);
SharedType* pSharedObj = (SharedType*)shmat(shmid, NULL, 0);
// pSharedObj->Futex = 0;
//fprintf(fLog, "%d %ld\n", pSharedObj->Index, tv.tv_usec);
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0)
{
cout << "pthread_setschedparam failed!" << endl;
getchar();
}
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(CPU_NUM, &mask);
//SharedType* pSharedObj = (SharedType*)pObj;
struct timeval tv1,tv2;
struct timezone tz1,tz2;
long int starttime,endtime;
// int rtn = 0;
for (int i = 0; i < 500; ++i)
{
if (sched_setaffinity(0, sizeof(mask), &mask) != 0)
{
cout << "pthread_setaffinity_np failed!" << endl;
getchar();
}
sem_wait(&pSharedObj->Sem12);
time_t curtime=time(0);
tm tim =*localtime(&curtime);
int hour,minu,sec;
hour=tim.tm_hour;
minu=tim.tm_min;
sec=tim.tm_sec;
gettimeofday(&tv1, &tz1);
one_ms();
gettimeofday(&tv2, &tz2);
starttime=tv1.tv_sec*1000000+tv1.tv_usec;
endtime=tv2.tv_sec*1000000+tv2.tv_usec;
printf("%d:%d:%d\n",hour,minu,sec);
printf("start time is %ld\t",starttime);
printf("end time is %ld\t",endtime);
printf("time slaped: %ld(us)\n", (endtime-starttime));
fprintf(fLog, "%d:%d:%d\t%ld\t%ld\n", hour,minu,sec,starttime,endtime);
fflush(fLog);
sched_yield();
}
fclose(fLog);
return 0;
}
»¹ÓÐpostµÄ´úÂëûÓÐÌù³öÀ´
Çó´óÉñÖ¸½Ì |
|