IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Linux | Open source  >

RunTime: Block memory copy

High performance programming techniques on Linux and Windows

developerWorks
文档选项

未显示需要 JavaScript 的文档选项


级别: 初级

Dr. Edward G. Bradford (egb@us.ibm.com), Senior Programmer, IBM

2001 年 6 月 01 日

A sample application that computes a single fractal point.

fract2.cpp - computes a single fractal point
        
	#ifdef _WIN32
	#include <windows.h>
	#else
	#define __int64 long long
	#include <sys/time.h>
	#include <stdlib.h>
	#endif
	//
	//	cl /O2 fract.cpp
	//
	#include <stdio.h>
	void tstart(void);
	void tend(void);
	double tval(void);
	int main(int ac, char *av[])
	{
		double a,b,c,d;
		double x,y,x1,y1;
		unsigned long iterations;
		unsigned long starttime, endtime;
		double tim;
		iterations = 100000000;
		if(ac != 5) {
			a = .1;
			b = .1;
			c = .1;
			d = .1;
		}
		else {
			a = atof(av[1]);
			b = atof(av[2]);
			c = atof(av[3]);
			d = atof(av[4]);
			if(ac > 5)
				iterations = atoi(av[5]);
		}
		unsigned long i;
		//
		//	x1 = x0*x0 + c
		//
		//	where x0 and c are complex numbers.
		//	x0 = a + bi;
		//	c  = c + di;
		//
		tstart();
		x = a;
		y = b;
		for(i = 0; i < iterations; i++) {
			x1 = x*x - y*y;
			y1 = -2 * x * y;
			if( (x1*x1 + y1*y1) > 1.0)
				break;
			x = x1 + c;
			y = y1 + d;
		}
		tend();
		tim = tval();
		printf("fract %13.9f %13.9f %13.9f %13.9f iter=%u    time %8.3f seconds\n",
				a,b,c,d,i,tim);
		return 0;
	}
	#ifdef _WIN32
	static LARGE_INTEGER _tstart, _tend;
	static LARGE_INTEGER freq;
	void tstart(void)
	{
		static int first = 1;
		if(first) {
			QueryPerformanceFrequency(&freq);
			first = 0;
		}
		QueryPerformanceCounter(&_tstart);
	}
	void tend(void)
	{
		QueryPerformanceCounter(&_tend);
	}
	double tval()
	{
		return ((double)_tend.QuadPart -
					(double)_tstart.QuadPart)/((double)freq.QuadPart);
	}
	#else
	static struct timeval _tstart, _tend;
	static struct timezone tz;
	void tstart(void)
	{
		gettimeofday(&_tstart, &tz);
	}
	void tend(void)
	{
		gettimeofday(&_tend,&tz);
	}
	double tval()
	{
		double t1, t2;
		t1 =  (double)_tstart.tv_sec + (double)_tstart.tv_usec/(1000*1000);
		t2 =  (double)_tend.tv_sec + (double)_tend.tv_usec/(1000*1000);
		return t2-t1;
	}
	#endif
      



关于作者

Dr. Edward G. Bradford has authored this article




对本文的评价










回页首


IBM 公司保留在 developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 提交转载请求表单 联系我们的编辑团队。
    关于 IBM 隐私条约 联系 IBM 使用条款