运用c语言在linux系统下减少对程序计算时间,急原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,// How to game the OS into not counting your computation:// Strategy is to do a little work a
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/01 06:04:50
![运用c语言在linux系统下减少对程序计算时间,急原程序代码如下,没太看懂是如何办到的,还有该方法的移植应该如何处理,// How to game the OS into not counting your computation:// Strategy is to do a little work a](/uploads/image/z/10281759-15-9.jpg?t=%E8%BF%90%E7%94%A8c%E8%AF%AD%E8%A8%80%E5%9C%A8linux%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%87%8F%E5%B0%91%E5%AF%B9%E7%A8%8B%E5%BA%8F%E8%AE%A1%E7%AE%97%E6%97%B6%E9%97%B4%2C%E6%80%A5%E5%8E%9F%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81%E5%A6%82%E4%B8%8B%2C%E6%B2%A1%E5%A4%AA%E7%9C%8B%E6%87%82%E6%98%AF%E5%A6%82%E4%BD%95%E5%8A%9E%E5%88%B0%E7%9A%84%2C%E8%BF%98%E6%9C%89%E8%AF%A5%E6%96%B9%E6%B3%95%E7%9A%84%E7%A7%BB%E6%A4%8D%E5%BA%94%E8%AF%A5%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86%2C%2F%2F+How+to+game+the+OS+into+not+counting+your+computation%3A%2F%2F+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 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可能会引起一些麻烦.