- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写 Langton 的 Ant sim(用于规则字符串 RLR)并试图优化它以提高速度。这是目前的相关代码:
#define AREA_X 65536
#define AREA_Y 65536
#define TURN_LEFT 3
#define TURN_RIGHT 1
int main()
{
uint_fast8_t* state;
uint_fast64_t ant=((AREA_Y/2)*AREA_X) + (AREA_X/2);
uint_fast8_t ant_orientation=0;
uint_fast8_t two_pow_five=32;
uint32_t two_pow_thirty_two=0;/*not fast, relying on exact width for overflow*/
uint_fast8_t change_orientation[4]={0, TURN_RIGHT, TURN_LEFT, TURN_RIGHT};
int_fast64_t* move_ant={AREA_X, 1, -AREA_X, -1};
... initialise empty state
while(1)
{
while(two_pow_five--)/*removing this by doing 32 steps per inner loop, ~16% longer*/
{
while(--two_pow_thirty_two)
{
/*one iteration*/
/* 54 seconds for init + 2^32 steps
ant_orientation = ( ant_orientation + (117>>((++state[ant])*2 )) )&3;
state[ant] = (36 >> (state[ant] *2) ) & 3;
ant+=move_ant[ant_orientation];
*/
/* 47 seconds for init + 2^32 steps
ant_orientation = ( ant_orientation + ((state[ant])==1?3:1) )&3;
state[ant] += (state[ant]==2)?-2:1;
ant+=move_ant[ant_orientation];
*/
/* 46 seconds for init + 2^32 steps
ant_orientation = ( ant_orientation + ((state[ant])==1?3:1) )&3;
if(state[ant]==2)
{
--state[ant];
--state[ant];
}
else
++state[ant];
ant+=move_ant[ant_orientation];
*/
/* 44 seconds for init + 2^32 steps
ant_orientation = ( ant_orientation + ((++state[ant])==2?3:1) )&3;
if(state[ant]==3)state[ant]=0;
ant+=move_ant[ant_orientation];
*/
// 37 seconds for init + 2^32 steps
// handle every situation with nested switches and constants
switch(ant_orientation)
{
case 0:
switch(state[ant])
{
case 0:
ant_orientation=1;
state[ant]=1;
++ant;
break;
case 1:
ant_orientation=3;
state[ant]=2;
--ant;
break;
case 2:
ant_orientation=1;
state[ant]=0;
++ant;
break;
}
break;
case 1:
switch(state[ant])
{
...
}
break;
case 2:
switch(state[ant])
{
...
}
break;
case 3:
switch(state[ant])
{
...
}
break;
}
}
}
two_pow_five=32;
... dump to file every 2^37 steps
}
return 0;
}
我有两个问题:
我已尝试通过反复试验来尽可能优化 c,是否有任何我没有利用的技巧?请尝试用 c 而不是汇编来交谈,尽管我可能会在某个时候尝试汇编。
是否有更好的方法对问题进行建模以提高速度?
更多信息:便携性并不重要。我在 64 位 linux 上,使用 gcc、i5-2500k 和 16 GB 内存。目前的状态数组使用 4GiB,程序可以使用 12GiB 的 ram。 sizeof(uint_fast8_t)=1。有意不存在边界检查,很容易从转储中手动发现损坏。
编辑:也许与直觉相反,堆积 switch 语句而不是消除它们已经产生了迄今为止最好的效率。
编辑:我重新建模了问题并提出了比每次迭代一个步骤更快的方法。以前,每个状态元素使用两位并描述朗顿 Ant 网格中的单个单元格。新方法使用所有 8 位,并描述网格的 2x2 部分。每次迭代都会完成可变数量的步骤,方法是查找步数、新方向和当前状态+方向的新状态的预先计算值。假设一切都同样可能,它平均每次迭代采取 2 个步骤。作为奖励,它使用 1/4 的内存来模拟相同的区域:
while(--iteration)
{
// roughly 31 seconds per 2^32 steps
table_offset=(state[ant]*24)+(ant_orientation*3);
it+=twoxtwo_table[table_offset+0];
state[ant]=twoxtwo_table[table_offset+2];
ant+=move_ant2x2[(ant_orientation=twoxtwo_table[table_offset+1])];
}
还没有尝试优化它,接下来要尝试的是像以前一样使用嵌套开关和常量消除偏移方程和查找(但使用 648 个内部案例而不是 12 个)。
最佳答案
或者,您可以使用单个无符号字节常量作为人工寄存器而不是分支:
value: 1 3 1 1
bits: 01 11 01 01 ---->101 decimal value for an unsigned byte
index 3 2 1 0 ---> get first 2 bits to get "1" (no shift)
--> get second 2 bits to get "1" (shifting for 2 times)
--> get third 2 bits to get "3" (shifting for 4 times)
--> get last 2 bits to get "1" (shifting for 6 times)
Then "AND" the result with binary(11) or decimal(3) to get your value.
(101>>( (++state[ant])*2 ) ) & 3 would give you the turnright or turnleft
Example:
++state[ant]= 0: ( 101>>( (0)*2 ) )&3 --> 101 & 3 = 1
++state[ant]= 1: ( 101>>( (1)*2 ) )&3 --> 101>>2 & 3 = 1
++state[ant]= 2: ( 101>>( (2)*2 ) )&3 --> 101>>4 & 3 = 3 -->turn left
++state[ant]= 3: ( 101>>( (3)*2 ) )&3 --> 101>>6 & 3 = 1
Maximum six-shifting + one-multiplication + one-"and" may be better.
Dont forget constant can be auto-promoted so you may add some suffixes or something else.
由于您对 %4 模数使用“unsigned int”,因此您可以使用“and”运算。
state[ant]=state[ant]&3; instead of state[ant]=state[ant]%4;
对于不熟练的编译器,这应该会提高速度。
最难的部分:modulo-3
C = A % B is equivalent to C = A – B * (A / B)
We need state[ant]%3
Result = state[ant] - 3 * (state[ant]/3)
state[ant]/3 is always <=1 for your valid direction states.
Only when state[ant] is 3 then state[ant]/3 is 1, other values give 0.
When multiplied by 3, that part is 0 or 3 (only 3 when state[ant] is 3 otherwise 0)
Result = state[ant] - (0 or 3)
Lets look at all possibilities:
state[ant]=0: 0 - 0 ---> 0 ----> 00100100 shifted by 0 times &3 --> 00000000
state[ant]=1: 1 - 0 ---> 1 ----> 00100100 shifted by 2 times &3 --> 00000001
state[ant]=2: 2 - 0 ---> 2 ----> 00100100 shifted by 4 times &3 --> 00000010
state[ant]=3: 3 - 3 ---> 0 ----> 00100100 shifted by 6 times &3 --> 00000000
00100100 is 36 in decimal.
(36 >> (state[ant] *2) ) & 3 will give you state[ant]%3 for your valid states (0,1,2,3)
Example:
state[ant]=0: 36 >> 0 --> 36 ----> 36& 3 ----> 0 satisfies 0%3
state[ant]=1: 36 >> 2 --> 9 -----> 9 & 3 ----> 1 satisfies 1%3
state[ant]=2: 36 >> 4 --> 2 -----> 2 & 3 ----> 2 satisfies 2%3
state[ant]=3: 36 >> 6 --> 0 -----> 0 & 3 ----> 0 satisfies 3%3
关于c - 如何优化这个 Langton's ant sim?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599613/
这个问题可能不是很清楚,所以让我用一个例子来说明我的意思。说我要复制几个文件夹: ... 但是我需要从如下所示的文本文件中加载它们,而不是在脚本中对这些文件夹
ant 和 ant clean all 的区别? 请任何人都可以清楚地说明何时使用 Ant 和 Ant 清洁所有。 C:> Ant c:> Ant 清理所有 最佳答案 “ant”运行项目的默认目标。
我想转换如下: 到: aoeu 的值可以包含任意数量的逗号分隔元素。 我可以使用 groovy Ant 任务,但不能使用 ant-contrib 中的任何
我看到了 this相关问题,但我的情况不同,所以再次询问。基本上,我必须按特定顺序运行 12 个 ant 文件。对于每个 ant 文件,我选择不同的目标,例如“创建”或“构建并部署全部”。如何创建一个
我可以编写一个在从另一个 ant 任务执行时获取参数的 ant 任务吗? 我通常试图实现的目标是重用现有任务不同的参数。 我不知道的是: ant中有这样的子任务吗? 它可以带参数吗? 如何以及在何处指
给定一个 ant 文件集,我需要对其执行一些类似 sed 的操作,将其压缩为多行字符串(每个文件有效一行),并将结果输出到文本文件。 我在寻找什么 Ant 任务? 最佳答案 Ant script ta
我有一个包含 jars 等绝对路径的属性文件。当使用这些属性时,它们以构建文件中指定的 basedir 为前缀。 我如何获得绝对路径? build.properties: mylib=/lib/myl
我有一个任务: someString someOtherString 如何连接 s
我遇到的情况涉及运行带有可选参数的 ant 构建,这些参数总是被指定但并不总是被定义,就像这样 ant -DBUILD_ENVIRONMENT=test -Dusername_ext= -Dconf.
我正在寻找一种在 Ant 文件中包含 .jar 的方法,以便我可以立即使用它并在我的目标中调用它的方法。 就我而言,它是 ant-contrib-1.0b3.jar . 最佳答案 最好的方法是将 An
我在 ant 方面比较新,在学校我有一个作业来做一个构建文件。我的问题之一是将其名称(或路径)作为 ant 参数的文件复制到“/foldercopy”。我需要做类似的事情: Ant cpfile文件.
亲爱的,我目前在检索foreach循环中设置的属性的值时遇到一些问题。也许你们中的一个可以帮助我... 目的是检查自从生成相应的jar之后,是否已修改文件夹的一个文件。这样,我知道是否必须再次生成ja
我想创建一个宏: 然后使用它: 但是,我想为隐式元素指定一个默认值......类似于: 所以我可以这样使用它:
我想将 ANT、JavaSDK 和 FlexSDK 包含到我的项目目录中。我需要我公司的人能够从源代码编译。 我有一个以以下内容开头的 build.bat 文件: ant blah/blah/blah
我想对目录中的每个文件使用 ant 脚本集只读 但 exec 不允许 filelist: The typ
如果我以 root 身份运行任务,有没有办法检测它是否以 root 身份运行并以不同的用户身份运行某些任务。 我有一些任务需要以 root 身份运行,但其他任务只需要以当前用户身份运行。 最佳答案 如
是否可以通过ant任务使用JUnit 4.6的新MaxCore运行程序? 最佳答案 从4.6开始,不幸的是没有。您需要创建自己的自定义Ant任务才能利用MaxCore功能。 关于ant - Ant J
我有一个关于 Ant 及其对环境变量的处理的问题。 为了说明我有一个小样本。 给定 Ant 构建文件 test.xml:
该文件如下所示: a1,b1 a2,b2 ... 我知道值“a2”。 如何将值“b2”转换为属性值。 我知道如何通过以下方式选择包含“a2”的行: 但是不知道如何将属性值设置为“b2”。 我
Ant 属性可以通过属性文件设置,从属性文件解析其他属性吗? 例如,我可以这样做: 和 prop2 变成“in_test_xml1”。那挺好的。 但在这种情况下,当使用输入属性文件时: prop1
我是一名优秀的程序员,十分优秀!