是否有一个工具/解决编程的循环,其中的条件,才检查每隔X迭代? [英] Is there a tool/solution to program a loop where the condition is only checked every X Iterations?

查看:137
本文介绍了是否有一个工具/解决编程的循环,其中的条件,才检查每隔X迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:我有一个由while循环(这人会检查素数)的函数

For example: I have a function consisting of a while loop (this one would check for primes)

function isprime(int number){
int i=2;
int max= (int) sqrt(number)+1;
   while(i<max){
       if(number%i==0){
          return 0;
       }
       i++;
   }
return 1;
}

我知道这将是测试素数一个非常贫穷的算法,但我的问题更关注环反正。目前该功能的操作的规定是公正检查条件。对于较大的数字,这可能是一个可怕的很多。

I know this would be a very poor algorithm for testing primes, but my question focuses more on loops anyway. Currently a forth of the function's operations are "just" checking the condition. For larger numbers, this can be an awful lot.

(快速例1:如果数字是1009,那会被检查,而状态的30倍,该指数30操作,29 * 2操作了,如果条件是118业务)

(Quick Example 1: If "number" is 1009, that would be checking the while condition 30 times, 30 operations for the index, and 29*2 operations for the if condition. That's 118 operations)

我认识到,我可以剪切和放大器; while条件中粘贴和具有该指数通过最大值,并导致额外​​的操作,同时,不伪造的返回值。所以,如果我切一切从如果...来启动我++;并将其粘贴三(或n)次,检查而条件只会占用的操作的1/10(或1 /(1 + 3N),同时创造一个最大的+ 2 * 3(或+(N-1 )* 3)不必要的操作。

I realize that I can just cut&paste within the while condition and having the the index pass the maximum, while resulting in additional operations, doesn't falsify the return value. So if I cut everything starting from "if..." to "i++;" and paste it three (or n) times, checking the while condition would only take up 1/10 (or 1/(1+3n) of the operations, while creating a maximum of +2*3(or +(n-1)*3) unnecessary operations.

(快速实施例2:如果数字是1009,这将意味着检查而条件11次,为索引33的操作,和33 * 2操作为如果条件那100的操作,13以下)的

(Quick Example 2: If "number" is 1009, that would mean checking the while condition 11 times, 33 operations for the index, and 33*2 operations for the if condition. That's 100 operations, 13 less)

由于我目前非常大的数字实验(通俗地说:状态将是一个非常,非常,非常长的时间假),以便粘贴,如果条件和增量上千次将是有益的,但非常不切实际的 - 所以我的问题是:

As I am currently experimenting with very big numbers (in layman's terms: "the condition will be false for a very, very, very long time") so pasting the if condition and the increment thousands of times would be useful, but very impractical - so my question is:

是否有一个工具(或我缺少的一种技术)来为我做的,但保持code清晰,易于修改?

Is there a tool (or a technique I am missing) to do that for me, but keeps the code clearly and easy to modify?

在此先感谢!

推荐答案

您的问题是有点不清楚。

Your question is a bit unclear.

首先,你可以稍微你的算法改变;例如2递增,而不是1(因为上述每2素数是奇数)。

First, you could change slightly your algorithm; e.g. incrementing by 2, not by 1 (since every prime number above 2 is odd).

随后,当记者问到优化(例如,使用 G ++ -Wall -O2 ),编译器通常做一些循环展开,仿佛它复制(和恒定折叠)一个循环体多次。

Then, when asked to optimize (e.g. with g++ -Wall -O2), a compiler generally do some loop unrolling, as if it "copied" (and constant-folded) a loop's body several times.

使用 GCC ,你可能会有助于例如优化使用 __builtin_expect ,例如以

With GCC, you might help the optimizer by e.g. using __builtin_expect, e.g. with

#ifdef __GNUC__
#define MY_UNLIKELY(P) __builtin_expect((P),0)
#else
#define MY_UNLIKELY(P) (P)
#endif 

,然后你会code

and then you'll code

 if(MY_UNLIKELY(number%i==0))
   return 0;

最后,手动优化code可能不值得的痛苦,你应该标杆。 (今天 CPU缓存是非常重要的,所以展开过多可能会减慢code ,也见 __ builtin_ prefetch GCC 并这个)。

At last, manually optimizing your code might not worth the pain, you should benchmark. (The CPU cache is very important today, so unrolling too much might slow down the code, see also __builtin_prefetch in GCC and this).

您也可以考虑元编程和的多级编程设施;像元ocaml的或的 Common Lisp的元编程手段的更多的比C ++ 11 模板的东西。你可以在运行时考虑生成C code (当时的 的dlopen ING它),或使用JIT编译库,例如 libjit (例如做的部分评价)。另请参阅J.Pitrat的关于<一个博客href=\"http://bootstrappingartificialintelligence.fr/Word$p$pss3/2014/10/the-meta-combinatorial-search-much-better-than-the-combinatorial-search/\"相对=nofollow>元组合搜索和Wiki页面上 EVAL 。我 MELT 系统显示,这些技术可以(痛苦)是用C使用相关++(MELT生成C ++ code。在运行时,以便做的元编程这种方式)。

You could also consider meta-programming and multi-staged programming facilities; in languages like Meta Ocaml or Common Lisp meta-programming means much more than C++11 template things. You could consider generating C code at runtime (then dlopening it), or using JIT compilation libraries like libjit (e.g. do partial evaluation). Read also J.Pitrat's blog about meta-combinatorial search, and wikipage on eval. My MELT system shows that these techniques can (painfully) be used in relation with C++ (MELT generates C++ code at runtime so do meta-programming this way).

这篇关于是否有一个工具/解决编程的循环,其中的条件,才检查每隔X迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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