运用c语言在linux系统下减少对程序计算时间,急原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,// How to game the OS into not counting your computation:// Strategy is to do a little work a
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/31 23:47:37
运用c语言在linux系统下减少对程序计算时间,急原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,// How to game the OS into not counting your computation:// Strategy is to do a little work a
运用c语言在linux系统下减少对程序计算时间,急
原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,
// How to game the OS into not counting your computation:
// Strategy is to do a little work and then sleep until the next tick.
// this works because nanosleep(zero,NULL) sleeps until the next
// accounting tick!So you can do a little work and then
// sleep until *after* the clock has ticked!
#define _POSIX_C_SOURCE 199309
#include
#include
#include
struct timespec zero = {0,0}; // sleep for zero nanoseconds=after next tick!
typedef long cycle_t;
// a sneaky trick to get the number of elapsed cycles of the high-resolution
// clock really quickly by dropping into assembler.Much faster than
// clock_gettime(2) system call.
inline cycle_t get_cycles()
{
cycle_t ret;
asm volatile("rdtsc" :"=A" (ret));
return ret;
}
// calculate the cycles of the high resolution clock per accounting clock tick,
// by waiting through 1000 ticks and dividing.
cycle_t cycles_per_tick()
{
int i;
nanosleep(&zero,NULL); // sync with tick
cycle_t start = get_cycles();
for(i=0 ; i= work ) {// done enough work; wait for tick
nanosleep(&zero,NULL); // avoid bill; wait till after next tick
tick_start = get_cycles(); // start over
}
// do some short work here...
\x05for (i=0; i
运用c语言在linux系统下减少对程序计算时间,急原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,// How to game the OS into not counting your computation:// Strategy is to do a little work a
不知道你代码的目的,不好说你要怎样.
代码也没什么内容,因子的范围也搞不清楚.
另外你需要移植到什么平台?
MingW/Windows应该可以直接运行,多核处理器和SpeedStep可能会引起一些麻烦.