⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genhao.cpp

📁 牛顿迭代法开根号 比库函数sqrt()快
💻 CPP
字号:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>

#include"Timer.h"
using namespace std;
int main()
{
	double n,x1,divider=2.0;
	double p=0.414213562, q =0.594669914 ,sqrt2=1.414213562, one=1;
	double tmp,y;
	double t1,t2;
	Timer mytimer1,mytimer2;
    srand(time(NULL));   	
  
	mytimer1.reset();
	mytimer1.start();
    for(int i=1;i<=1000000;i++)
	{
		n=rand()%100000-30000;
		
		n=sqrt(n);
		
	}mytimer1.stop();
    mytimer1.running();
    t1=mytimer1.seconds();
    cout<<"库函数的运算时间是: "<<t1<<endl;

    mytimer1.reset();
	mytimer2.start();
    for(int i=1;i<=1000000;i++)
	{
		n=rand()%100000-30000;

	 _asm
	{
		finit
		fldz
		fld n
		fcomp 
		fstsw ax
		sahf
		jb out1
 	
		ffree st(0)
		fld n
	    fxtract 
        fst tmp
		fmul p
		fadd q
		fstp x1
		fstp y

		mov cx,0003h
        loop1:    
		fld x1
		fld tmp
		fdiv st(0),st(1)
		fadd x1
		fdiv divider
        fstp x1
		dec cx
		jnz loop1

		fld divider
		fld y
        fprem 
		fld one
		fcomp 
	    fstsw ax
		sahf
		je next1
        
		fld y
		fdiv divider
		fld x1
		fscale
		fstp x1
		jmp out1
       
        next1:
		fld y
		fdiv divider
        fld x1
		fmul sqrt2
        fscale
		fstp x1
        out1:
	}
	 
	}
	mytimer2.stop();
	t2=mytimer2.seconds();

	cout<<"实验代码的运行时间是: "<<t2<<endl;
	cout<<"实验代码运行时间 / 库函数运行时间 = "<<t2/t1<<endl;
	system("pause");
	return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -