闪电计算器 [英] Lightning Calculator

查看:112
本文介绍了闪电计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题,你怎么定义算术?,当我来到一个答案时,我以一种新的,更清晰的方式理解数学。我还发生了一个算术优化的方法,它确实加速了计算机上的计算。


如果你想定义数学我首先定义加法。

定义加法的最简单方法是为每个

数创建一个函数,并说f_n(x)= x + n,其中n是你预先形成的数字

加上on,x是修改值。这个问题是它

使用加法来定义自己,所以我决定用它来写它:


f_1(x)=如果x == 1然后y = 2,如果x == 2则y = 3,如果x == 3则y = 4 ...

f_2(x)=如果x == 1则y = 3,如果x == 2则y = 4,如果x == 3则y = 5 ...

f_3(x)=如果x == 1则y = 4,如果x == 2那么y = 5,如果x == 3那么y = 6 ......

....


你可以为你希望的每个数字做到这一点使用和定义您希望使用的numers范围。

范围。我当然没有

创建了一个上升到无穷大的表,或者一个可以使用无限小数位的
。如果我需要,我将创建新功能,

使用更大的数字和更多的小数位。


我在计算机编程方面发现的是,当我手动

为变量赋值时,我不需要使用运算符来进行算法。我喜欢这样做是因为它更快。


这个C ++程序就是这种优化的一个例子。线条

注释掉预先形成与变量上预先形成算术的行完全相同的函数,但注释行很多

更快。


您认为我们可以优化计算机架构以利用这个吗?

欢迎对此理论提出任何反馈意见


#include< cstdlib>

#include< iostream>

#include< ctime>


使用命名空间std;


int main(int argc,char * argv [])

{


time_t t1,t0;

已过去两倍;


时间(& t0); / *开始时间* /

for(int cnt = 0; cnt< 1000000000; cnt ++){

int x = 0;

int y = 577;

x = x + y;

y = x + y;

// if(x == 0&& y == 577)x = 577;

// if(x == 577& y == 577)y = 1154;


}

时间(& t1);

逝去= difftime(t1,t0);

cout<<< elapsed;

system(PAUSE);

返回EXIT_SUCCESS;


}

I asked the question, "how do you define arithmetic?", and when I came
to an answer I understood math in a new, much clearer way. I also
happened upon a method of arithmetic optimization that really speeds up
calculations on computers.

If you wanted to define mathematics I would first define addition. The
simplest way to define addition is to create a function for every
number and say f_n(x)=x+n , where n is the number you are preforming
addition on and x is the modifying value. The problem with this is it
uses addition to define itself, so I decided to write it longhand:

f_1(x) = if x==1 then y=2 , if x==2 then y=3, if x==3 then y=4...
f_2(x) = if x==1 then y=3 , if x==2 then y=4, if x==3 then y=5...
f_3(x) = if x==1 then y=4 , if x==2 then y=5, if x==3 then y=6...
....

You could do this for every number you wished to use and define the
scope of the numers you wished to work with. I of course have not
created a table that goes up to infinity or one that can use infinite
decimal places. If I need to I will just create new functions that
work with larger numbers and more decimal places.

What I found in terms of computer programming, is that when I manually
assign values to variables I don''t need to use the operators to do
arithmetic. What I like about doing it this way is it is faster.

This C++ program is an example of this kind of optimization. The lines
commented out preform the exact same function as the lines which
preform arithmetic on the variables, but the commented lines are much
faster.

Do you think we could optimize computer architecture to take advantage
of this?
Any feedback on this theory is welcome

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{

time_t t1, t0;
double elapsed;

time(&t0); /* start time */
for(int cnt=0; cnt<1000000000; cnt++){
int x=0;
int y=577;
x=x+y;
y=x+y;
//if(x==0 && y==577) x=577;
//if(x==577 && y==577) y=1154;

}
time(&t1);
elapsed = difftime(t1, t0);
cout<<elapsed;
system("PAUSE");
return EXIT_SUCCESS;

}

推荐答案

Co ******** @ gmail.com 写道:

....
Co********@gmail.com wrote:
....
这个C ++程序就是这种优化的一个例子。
注释掉的行预先形成与对变量进行算术运算的行完全相同的函数,但注释行更快。
This C++ program is an example of this kind of optimization. The lines
commented out preform the exact same function as the lines which
preform arithmetic on the variables, but the commented lines are much
faster.



注释代码与添加代码的效果不同。


返回绘图板。


BTW - 如果这有很大的性能差异(当你得到正确的代码时),我会非常惊讶。



The commented code does not have the same effect as the addition code.

Back to the drawing board.

BTW - I would be very surprised if this has a really significant
performance difference (when you get the code right).


Co ******** @ gmail.com 写道:
Co********@gmail.com wrote:
I问了这个问题,你怎么定义算术?,当我来到答案时,我以一种新的,更清晰的方式理解数学。我还发生了一种算法优化的方法,它真正加速了计算机上的计算。

如果你想定义数学,我首先要定义加法。定义加法的最简单方法是为每个
数创建一个函数,并说f_n(x)= x + n,其中n是你要预编码的数字
另外,x是修改值。这个问题是它使用了自我定义,所以我决定用它来写它:

f_1(x)=如果x == 1那么y = 2,如果x = = 2然后y = 3,如果x == 3则y = 4 ...
f_2(x)=如果x == 1则y = 3,如果x == 2则y = 4,如果x == 3然后y = 5 ...
f_3(x)=如果x == 1则y = 4,如果x == 2则y = 5,如果x == 3则y = 6 .. 。
...

您可以为您希望使用的每个数字执行此操作,并定义您希望使用的数字范围。我当然没有创建一个上升到无穷大的表或一个可以使用无限小数位的表。如果我需要,我将创建新的功能,使用更大的数字和更多的小数位。

我在计算机编程方面发现的是,当我手动
时为变量赋值我不需要使用运算符来进行算术运算。我喜欢这样做是因为它更快。

这个C ++程序就是这种优化的一个例子。
注释掉的行预先形成与对变量进行算术运算的行完全相同的功能,但注释行的速度要快得多。

你觉得吗?我们可以优化计算机体系结构以利用这个吗?
欢迎任何关于这个理论的反馈

#include< cstdlib>
#include< iostream>
#include< ctime>

使用命名空间std;

int main(int argc,char * argv [])
{

time_t t1,t0;
已过去;

时间(& t0); / *开始时间* /
for(int cnt = 0; cnt< 1000000000; cnt ++){
int x = 0;
int y = 577;
x = x + y;
y = x + y;
// if(x == 0&& y == 577)x = 577;
// if(x == 577& ;& y == 577)y = 1154;

}
时间(& t1);
elapsed = difftime(t1,t0);
cout< ;< elapsed;
系统(暂停);
返回EXIT_SUCCESS;

}
I asked the question, "how do you define arithmetic?", and when I came
to an answer I understood math in a new, much clearer way. I also
happened upon a method of arithmetic optimization that really speeds up
calculations on computers.

If you wanted to define mathematics I would first define addition. The
simplest way to define addition is to create a function for every
number and say f_n(x)=x+n , where n is the number you are preforming
addition on and x is the modifying value. The problem with this is it
uses addition to define itself, so I decided to write it longhand:

f_1(x) = if x==1 then y=2 , if x==2 then y=3, if x==3 then y=4...
f_2(x) = if x==1 then y=3 , if x==2 then y=4, if x==3 then y=5...
f_3(x) = if x==1 then y=4 , if x==2 then y=5, if x==3 then y=6...
...

You could do this for every number you wished to use and define the
scope of the numers you wished to work with. I of course have not
created a table that goes up to infinity or one that can use infinite
decimal places. If I need to I will just create new functions that
work with larger numbers and more decimal places.

What I found in terms of computer programming, is that when I manually
assign values to variables I don''t need to use the operators to do
arithmetic. What I like about doing it this way is it is faster.

This C++ program is an example of this kind of optimization. The lines
commented out preform the exact same function as the lines which
preform arithmetic on the variables, but the commented lines are much
faster.

Do you think we could optimize computer architecture to take advantage
of this?
Any feedback on this theory is welcome

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{

time_t t1, t0;
double elapsed;

time(&t0); /* start time */
for(int cnt=0; cnt<1000000000; cnt++){
int x=0;
int y=577;
x=x+y;
y=x+y;
//if(x==0 && y==577) x=577;
//if(x==577 && y==577) y=1154;

}
time(&t1);
elapsed = difftime(t1, t0);
cout<<elapsed;
system("PAUSE");
return EXIT_SUCCESS;

}




声音对我不好。这里有两个例程:


typedef unsigned char u_char;


inline

u_char sum(u_char a,u_char b){

如果(a == 0&& b == 0)返回0;

if(a == 0&& b == 1)返回1;

if(a == 0&& b == 2)返回2;

if(a == 0&& b == 3)返回3;

如果(a == 0&& b == 4)返回4;

if(a == 0&& ; b == 5)返回5;

if(a == 0&& b == 6)返回6;

if(a == 0& ;& b == 7)返回7;


if(a == 1&& b == 0)返回1;

if (a == 1&& b == 1)返回2;

如果(a == 1&& b == 2)返回3;

如果(a == 1&& b == 3)返回4;

如果(a == 1&& b == 4)返回5;

if(a == 1&& b == 5)return 6;

if(a == 1&& b == 6)return 7;

如果(a == 1&& b == 7)返回8;


if(a == 2&& b == 0)返回2;

if(a == 2&& b == 1)返回3;

if(a == 2& & b == 2)返回4;

如果(a == 2&& b == 3)返回5;

if(a == 2 && b == 4)返回6;

如果(a == 2&& b == 5)返回7;

if(a = = 2&& b == 6)返回8;

如果(a == 2&& b == 7)返回9;


if(a == 3&& b == 0)返回3;

if(a == 3&& b == 1)返回4;

如果(a == 3&& b == 2)返回5;

如果(a == 3&& b == 3)返回6;

if(a == 3&& b == 4)return 7;

if(a == 3&& b == 5)return 8;

如果(a == 3&& b == 6)返回9;

if(a == 3&& b == 7)return 10;


如果(a == 4&& b == 0)返回4;

if(a == 4&& b == 1)返回5;

如果(a == 4&& b == 2)返回6;

i f(a == 4&& b == 3)返回7;

如果(a == 4&& b == 4)返回8;

if(a == 4& & b == 5)返回9;

如果(a == 4&& b == 6)返回10;

if(a == 4 && b == 7)返回11;


返回0;


}


inline

u_char sum1(u_char a,u_char b){

return(a + b);

}


你期望sum1更快吗?你是对的,它没有添加,

然而它执行了不少测试,这也花费了时间。


最佳

Kai-Uwe Bux



Sounds bogus to me. Here are two routines:

typedef unsigned char u_char;

inline
u_char sum ( u_char a, u_char b ) {
if ( a == 0 && b == 0 ) return 0;
if ( a == 0 && b == 1 ) return 1;
if ( a == 0 && b == 2 ) return 2;
if ( a == 0 && b == 3 ) return 3;
if ( a == 0 && b == 4 ) return 4;
if ( a == 0 && b == 5 ) return 5;
if ( a == 0 && b == 6 ) return 6;
if ( a == 0 && b == 7 ) return 7;

if ( a == 1 && b == 0 ) return 1;
if ( a == 1 && b == 1 ) return 2;
if ( a == 1 && b == 2 ) return 3;
if ( a == 1 && b == 3 ) return 4;
if ( a == 1 && b == 4 ) return 5;
if ( a == 1 && b == 5 ) return 6;
if ( a == 1 && b == 6 ) return 7;
if ( a == 1 && b == 7 ) return 8;

if ( a == 2 && b == 0 ) return 2;
if ( a == 2 && b == 1 ) return 3;
if ( a == 2 && b == 2 ) return 4;
if ( a == 2 && b == 3 ) return 5;
if ( a == 2 && b == 4 ) return 6;
if ( a == 2 && b == 5 ) return 7;
if ( a == 2 && b == 6 ) return 8;
if ( a == 2 && b == 7 ) return 9;

if ( a == 3 && b == 0 ) return 3;
if ( a == 3 && b == 1 ) return 4;
if ( a == 3 && b == 2 ) return 5;
if ( a == 3 && b == 3 ) return 6;
if ( a == 3 && b == 4 ) return 7;
if ( a == 3 && b == 5 ) return 8;
if ( a == 3 && b == 6 ) return 9;
if ( a == 3 && b == 7 ) return 10;

if ( a == 4 && b == 0 ) return 4;
if ( a == 4 && b == 1 ) return 5;
if ( a == 4 && b == 2 ) return 6;
if ( a == 4 && b == 3 ) return 7;
if ( a == 4 && b == 4 ) return 8;
if ( a == 4 && b == 5 ) return 9 ;
if ( a == 4 && b == 6 ) return 10;
if ( a == 4 && b == 7 ) return 11;

return 0;

}

inline
u_char sum1 ( u_char a, u_char b ) {
return( a + b );
}

Would you expect sum1 to be faster? You are right that it does not add,
however it performs quite a few tests, which also cost time.

Best

Kai-Uwe Bux




< Co ******** @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

<Co********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
我问了一个问题,你如何定义算术?,当我来到答案时,我以一种新的,更清晰的方式理解数学。我还发生了一种算法优化的方法,它真正加速了计算机上的计算。

如果你想定义数学,我首先要定义加法。定义加法的最简单方法是为每个
数创建一个函数,并说f_n(x)= x + n,其中n是你要预编码的数字
另外,x是修改值。这个问题是它使用了自我定义,所以我决定用它来写它:

f_1(x)=如果x == 1那么y = 2,如果x = = 2然后y = 3,如果x == 3则y = 4 ...
f_2(x)=如果x == 1则y = 3,如果x == 2则y = 4,如果x == 3然后y = 5 ...
f_3(x)=如果x == 1则y = 4,如果x == 2则y = 5,如果x == 3则y = 6 .. 。
...

您可以为您希望使用的每个数字执行此操作,并定义您希望使用的数字范围。我当然没有创建一个上升到无穷大的表或一个可以使用无限小数位的表。如果我需要,我将创建新的功能,使用更大的数字和更多的小数位。

我在计算机编程方面发现的是,当我手动
时为变量赋值我不需要使用运算符来进行算术运算。我喜欢这样做是因为它更快。

这个C ++程序就是这种优化的一个例子。
注释掉的行预先形成与对变量进行算术运算的行完全相同的功能,但注释行的速度要快得多。

你觉得吗?我们可以优化计算机体系结构以利用这个吗?
欢迎任何关于这个理论的反馈

#include< cstdlib>
#include< iostream>
#include< ctime>

使用命名空间std;

int main(int argc,char * argv [])
{

time_t t1,t0;
已过去;

时间(& t0); / *开始时间* /
for(int cnt = 0; cnt< 1000000000; cnt ++){
int x = 0;
int y = 577;
x = x + y;
y = x + y;
// if(x == 0&& y == 577)x = 577;
// if(x == 577& ;& y == 577)y = 1154;

}
时间(& t1);
elapsed = difftime(t1,t0);
cout< ;< elapsed;
系统(暂停);
返回EXIT_SUCCESS;

}
I asked the question, "how do you define arithmetic?", and when I came
to an answer I understood math in a new, much clearer way. I also
happened upon a method of arithmetic optimization that really speeds up
calculations on computers.

If you wanted to define mathematics I would first define addition. The
simplest way to define addition is to create a function for every
number and say f_n(x)=x+n , where n is the number you are preforming
addition on and x is the modifying value. The problem with this is it
uses addition to define itself, so I decided to write it longhand:

f_1(x) = if x==1 then y=2 , if x==2 then y=3, if x==3 then y=4...
f_2(x) = if x==1 then y=3 , if x==2 then y=4, if x==3 then y=5...
f_3(x) = if x==1 then y=4 , if x==2 then y=5, if x==3 then y=6...
...

You could do this for every number you wished to use and define the
scope of the numers you wished to work with. I of course have not
created a table that goes up to infinity or one that can use infinite
decimal places. If I need to I will just create new functions that
work with larger numbers and more decimal places.

What I found in terms of computer programming, is that when I manually
assign values to variables I don''t need to use the operators to do
arithmetic. What I like about doing it this way is it is faster.

This C++ program is an example of this kind of optimization. The lines
commented out preform the exact same function as the lines which
preform arithmetic on the variables, but the commented lines are much
faster.

Do you think we could optimize computer architecture to take advantage
of this?
Any feedback on this theory is welcome

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main(int argc, char *argv[])
{

time_t t1, t0;
double elapsed;

time(&t0); /* start time */
for(int cnt=0; cnt<1000000000; cnt++){
int x=0;
int y=577;
x=x+y;
y=x+y;
//if(x==0 && y==577) x=577;
//if(x==577 && y==577) y=1154;

}
time(&t1);
elapsed = difftime(t1, t0);
cout<<elapsed;
system("PAUSE");
return EXIT_SUCCESS;

}




Corey是什么让你认为评论的行比添加的更快?
运算符?


据我所知,假设没有使用编译器优化,每个

注释行可以转换成两个减法(SUB)操作

(整数比较是减法和检查零),逻辑AND

操作,和一个非常慢(与加法和减法相比)跳转

(JMP)操作,然后是一个任务( MOV)操作。到目前为止,你有
x和y,(x == 0)的结果,(y == 577)的结果,(x == 0&& y = 577)的结果,

所有操作数都不太可能存储在寄存器中以便快速访问。


一个简单的添加只是一个添加(ADD)操作加上一个赋值

操作。并且因为只有x,y和x + y的结果被存储和读取(

结果是x + y自然地在ACC中),很可能是整个操作

在寄存器上完成,这意味着非常快。


通过编译器优化,编译器可能会看到x

和y的定义,然后分配给x和y,所以

迭代范围内的x和y的值是完全可预测的(x = 577和y = 1154),它是
不会无论如何都要添加。


最后,未注释的代码比您设计的代码更明确,更简单,更易读。


问候,

Ben



Corey, what makes you think the commented lines are faster than the addtion
operator?

As far as I know, and assuming no compiler optimization is used, each
commented line can be translated into two subtraction (SUB) operations
(comparison on integers is substraction and check zero), a logical AND
operation, and a very slow (as compared to addtion and subtraction) jump
(JMP) operation, followed by an assignment (MOV) operation. So far you have
x and y, result of (x==0), result of (y==577), result of (x==0 && y==577),
it is unlikely that all operands are stored in register for fast access.

A simple addtion is just an addtion (ADD) operation plus an assignment
operation. And because only x, y and result of x+y are stored and read (the
result of x+y naturally in the ACC), it is likely that the whole operation
is done on registers, which means very fast.

With compiler optimization, the compiler probably sees the definition of x
and y followed by assignment to x and y, so the values of x and y within the
iteration scope are entirely predictable (x = 577 and y = 1154), that it
wouldn''t bother to add anyway.

Finally, the uncommented code is much more explicit, simple, readable than
the one you devised.

Regards,
Ben


这篇关于闪电计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆