您的位置:首页 > 其它

VC中如何得到一个线程的效率

2006-05-24 14:39 274 查看
//定义开始时间
LARGE_INTEGER fre,star_,end_;
QueryPerformanceFrequency(&fre);
QueryPerformanceCounter(&star_);

// 处理....
pro( mb);

//得到结束的时间
QueryPerformanceCounter(&end_);
ACE_DEBUG((LM_INFO,ACE_TEXT(" (%t) 处理时间%i/n"),
long((end_.QuadPart - star_.QuadPart) * 10000 / fre.QuadPart)));

这个地方打印出来的时间是1个单位为0.1个毫秒

ACE_hrtime_t startTime = ACE_OS::gethrtime();

ACE_INLINE ACE_hrtime_t
ACE_OS::gethrtime (const ACE_HRTimer_Op op)
{
ACE_OS_TRACE ("ACE_OS::gethrtime");
#if defined (ACE_HAS_HI_RES_TIMER)
ACE_UNUSED_ARG (op);
return ::gethrtime ();
#elif defined (ACE_HAS_AIX_HI_RES_TIMER)
ACE_UNUSED_ARG (op);
timebasestruct_t tb;

::read_real_time(&tb, TIMEBASE_SZ);
::time_base_to_time(&tb, TIMEBASE_SZ);

return ACE_hrtime_t(tb.tb_high) * ACE_ONE_SECOND_IN_NSECS + tb.tb_low;
#elif defined (ACE_WIN32)
ACE_UNUSED_ARG(op);
LARGE_INTEGER freq;

::QueryPerformanceCounter (&freq);

# if defined (ACE_LACKS_LONGLONG_T)
ACE_UINT64 uint64_freq (freq.u.LowPart,
static_cast<unsigned int> (freq.u.HighPart));
return uint64_freq;
# else
return freq.QuadPart;
# endif //ACE_LACKS_LONGLONG_T
#elif defined (ghs) && defined (ACE_HAS_PENTIUM)
ACE_UNUSED_ARG (op);
// Use .obj/gethrtime.o, which was compiled with g++.
return ACE_GETHRTIME_NAME ();
#elif (defined (__GNUG__) || defined (__INTEL_COMPILER)) && !defined(ACE_VXWORKS) && defined (ACE_HAS_PENTIUM)
ACE_UNUSED_ARG (op);
# if defined (ACE_LACKS_LONGLONG_T)
double now;
# else /* ! ACE_LACKS_LONGLONG_T */
ACE_hrtime_t now;
# endif /* ! ACE_LACKS_LONGLONG_T */

// Read the high-res tick counter directly into memory variable "now".
// The A constraint signifies a 64-bit int.
asm volatile ("rdtsc" : "=A" (now) : : "memory");

# if defined (ACE_LACKS_LONGLONG_T)
ACE_UINT32 least, most;
ACE_OS::memcpy (&least, &now, sizeof (ACE_UINT32));
ACE_OS::memcpy (&most, (u_char *) &now + sizeof (ACE_UINT32),
sizeof (ACE_UINT32));

ACE_hrtime_t ret (least, most);
return ret;
# else /* ! ACE_LACKS_LONGLONG_T */
return now;
# endif /* ! ACE_LACKS_LONGLONG_T */
#elif defined (linux) && defined (ACE_HAS_ALPHA_TIMER)
// NOTE: alphas only have a 32 bit tick (cycle) counter. The rpcc
// instruction actually reads 64 bits, but the high 32 bits are
// implementation-specific. Linux and Digital Unix, for example,
// use them for virtual tick counts, i.e., taking into account only
// the time that the process was running. This information is from
// David Mosberger's article, see comment below.
ACE_UINT32 now;

// The following statement is based on code published by:
// Mosberger, David, "How to Make Your Applications Fly, Part 1",
// Linux Journal Issue 42, October 1997, page 50. It reads the
// high-res tick counter directly into the memory variable.
asm volatile ("rpcc %0" : "=r" (now) : : "memory");

return now;
#elif defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))
// PowerPC w/ GreenHills or g++.

ACE_UNUSED_ARG (op);
u_long most;
u_long least;

#if defined (ghs)
ACE_OS::readPPCTimeBase (most, least);
#else
u_long scratch;

do {
asm volatile ("mftbu %0/n"
"mftb %1/n"
"mftbu %2"
: "=r" (most), "=r" (least), "=r" (scratch));
} while (most != scratch);
#endif

#if defined (ACE_LACKS_LONGLONG_T)
return ACE_U_LongLong (least, most);
#else /* ! ACE_LACKS_LONGLONG_T */
return 0x100000000llu * most + least;
#endif /* ! ACE_LACKS_LONGLONG_T */

#elif defined (ACE_HAS_CLOCK_GETTIME)
// e.g., VxWorks (besides POWERPC && GreenHills) . . .
ACE_UNUSED_ARG (op);
struct timespec ts;

ACE_OS::clock_gettime (
#if defined (ACE_HAS_CLOCK_GETTIME_MONOTONIC)
CLOCK_MONOTONIC,
#endif /* !ACE_HAS_CLOCK_GETTIME_MONOTONIC */
CLOCK_REALTIME,
&ts);

// Carefully create the return value to avoid arithmetic overflow
// if ACE_hrtime_t is ACE_U_LongLong.
return static_cast<ACE_hrtime_t> (ts.tv_sec) *
ACE_U_ONE_SECOND_IN_NSECS + static_cast<ACE_hrtime_t> (ts.tv_nsec);
#else
ACE_UNUSED_ARG (op);
ACE_Time_Value const now = ACE_OS::gettimeofday ();

// Carefully create the return value to avoid arithmetic overflow
// if ACE_hrtime_t is ACE_U_LongLong.
return (static_cast<ACE_hrtime_t> (now.sec ()) * (ACE_UINT32) 1000000 +
static_cast<ACE_hrtime_t> (now.usec ())) * (ACE_UINT32) 1000;
#endif /* ACE_HAS_HI_RES_TIMER */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: