Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 如何加快计算速度_Performance_Math_Lua_Nested Loops - Fatal编程技术网

Performance 如何加快计算速度

Performance 如何加快计算速度,performance,math,lua,nested-loops,Performance,Math,Lua,Nested Loops,我编写了以下代码: combinationsstring = "List of Combinations" for a = 0, 65 do for b = 0, 52 do for c = 0, 40 do for d = 0, 28 do for e = 0, 19 do for f = 0, 11 do for g = 0,

我编写了以下代码:

combinationsstring = "List of Combinations"
for a = 0, 65 do
    for b = 0, 52 do
        for c = 0, 40 do
            for d = 0, 28 do
                for e = 0, 19 do
                    for f = 0, 11 do
                        for g = 0, 4 do
                            if (((1.15^a)-1)+((20/3)*((1.15^b)-1))
                               +((100/3)*((1.15^c)-1))+(200*((1.15^d)-1))
                               +((2000/3)*((1.15^e)-1))+((8000/3)*((1.15^f)-1))
                               +((40000/3)*((1.15^g)-1))) < 10000 then
                                combinationsstring = combinationsstring
                                    .."\n"..a..", "..b..", "..c..", "..d
                                    ..", "..e..", "..f..", "..g
                            end
                        end
                    end
                end
            end
        end
    end
end

local file = io.open("listOfCombinations.txt", "w")
file:write(combinationsstring)
file:close()
组合字符串=“组合列表” 对于a=0,65 do 对于b=0,52 do 对于c=0,40 do 对于d=0,28 do 对于e=0,19 do 对于f=0,11 do 对于g=0,4 do 如果((1.15^a)-1)+(20/3)*((1.15^b)-1)) +(100/3)*(1.15^c)-1)+(200*(1.15^d)-1) +((2000/3)*((1.15^e)-1))+((8000/3)*((1.15^f)-1)) +((40000/3)*((1.15^g)-1))<10000 组合字符串=组合字符串 ..“\n”..a.、.b.、.c.、.d .e.、.f.、.g 终止 终止 终止 终止 终止 终止 终止 终止 本地文件=io.open(“listOfCombinations.txt”、“w”) 文件:写入(组合字符串) 文件:close() 我需要找到符合以下等式的所有数据集

(((1.15^a)-1)+((20/3)*((1.15^b)-1))+
((100/3)*((1.15^c)-1))+(200*((1.15^d)-1))+
((2000/3)*((1.15^e)-1))+((8000/3)*((1.15^f)-1))+
((40000/3)*((1.15^g)-1))) < 10000
((1.15^a)-1)+((20/3)*((1.15^b)-1))+
(100/3)*(1.15^c)-1)+(200*(1.15^d)-1)+
((2000/3)*((1.15^e)-1))+((8000/3)*((1.15^f)-1))+
((40000/3)*((1.15^g)-1))<10000
每个变量(a-g)都是一个实整数。所以我计算了7个变量的最大值(当所有其他值都为0时,每个变量的最大值)。这些最大值分别为65、52、40、28、19、11和4(62=a、52=b等)

因此,我创建了7个嵌套for循环(如上面的代码所示),在中间块中,我测试了7个值,看看它们是否符合标准,如果是,它们被添加到一个字符串中。在代码结束时,程序将重写一个文件,并将最后一个字符串放入其中,其中包含所有可能的组合

该程序运行良好,但在整个模拟过程中进行了31亿次计算,通过一些测试,我发现我的计算机平均每秒计算3000次。这意味着总模拟时间约为12天5小时。我没有这段时间,所以我花了整个上午简化了要测试的等式,删除了不必要的代码,这就是我的最终结果

我使用嵌套for循环完成的这个方法是这里的最佳方法吗?如果是的话,有没有其他方法可以加快速度,如果没有,你能告诉我另一种方法吗


另外,我使用Lua是因为它是我最熟悉的语言,但如果您有其他建议/示例,请使用您的语言,我可以尝试为本程序优化它。

我不会说Lua,但这里有一些建议:

  • b
    上启动循环之前,计算并存储
    1.15^a-1
    ;可以叫它
    fooa
  • 同样,在
    c
    上开始循环之前,计算
    fooa+(20/3)*(1.15^b-1)
    ;可以称之为foob
  • 在开始每个循环之前做类似的事情
  • 例如,如果
    foob
    至少为
    10000
    ,则中断循环;里面的东西 只能使结果更大
  • 这在lua中可能是无用的或更糟糕的,但您真的需要将结果累积到字符串中吗?我不知道lua是如何表示字符串和进行连接的,但是连接可能会对您造成严重的伤害。尝试改用列表或数组数据结构

我还想补充一点,嵌套循环是一个非常合理的解决方案,经过上述修改后,我会这么做。

我不会说lua,但这里有一些建议:

  • b
    上启动循环之前,计算并存储
    1.15^a-1
    ;可以叫它
    fooa
  • 同样,在
    c
    上开始循环之前,计算
    fooa+(20/3)*(1.15^b-1)
    ;可以称之为foob
  • 在开始每个循环之前做类似的事情
  • 例如,如果
    foob
    至少为
    10000
    ,则中断循环;里面的东西 只能使结果更大
  • 这在lua中可能是无用的或更糟糕的,但您真的需要将结果累积到字符串中吗?我不知道lua是如何表示字符串和进行连接的,但是连接可能会对您造成严重的伤害。尝试改用列表或数组数据结构

我还想补充一点,嵌套循环是一个完全合理的解决方案,经过上述修改后,我会做的就是这样。

我建议使用一种静态语言来实现这种性质的暴力强制。我遇到了一个问题,我使用Python遇到了麻烦,但C++蛮力八进制循环方法在30秒内计算出解决方案。

< P>我推荐一种静态语言来强制这种性质的事物。我遇到了一个问题,我使用Python遇到了麻烦,但是C++蛮力8进制循环方法在30秒内计算出解决方案。

< P>因为你也要求用不同的语言解决问题,这里是一个快速而肮脏的C++程序,也包含了@ TMykLeBu的建议。
#include <iostream>
#include <fstream>
#include <cmath>

int main()
{
    std::ofstream os( "listOfCombinations.txt" );
    using std::pow;
    for( double a = 0; a <= 65; ++a ) {
        double aa = (pow(1.15, a) - 1);
        if ( aa > 10000 ) break;
        for( double b = 0; b <= 52; ++b ) {
            double bb = aa + (20/3) * (pow(1.15, b) - 1);
            if ( bb > 10000 ) break;
            for( double c = 0; c <= 40; ++c ) {
                double cc = bb + (100/3) * (pow(1.15, c) - 1);
                if ( cc > 10000 ) break;
                // The following line provides some visual feedback for the
                // user about the progress (it prints current a, b, and c
                // values).
                std::cout << a << "   " << b << "   " << c << std::endl;
                for( double d = 0; d <= 28; ++d ) {
                    double dd = cc + 200 * ( pow(1.15, d) - 1);
                    if ( dd > 10000 ) break;
                    for( double e = 0; e <= 19; ++e ) {
                        double ee = dd + (2000/3) * (pow(1.15, e) - 1);
                        if ( ee > 10000 ) break;
                        for( double f = 0; f <= 11; ++f ) {
                            double ff = ee + (8000/3) * (pow(1.15, f) - 1);
                            if ( ff > 10000 ) break;
                            for( double g = 0; g <= 4; ++g ) {
                                double gg = ff + (40000/3) * (pow(1.15, g) - 1);
                                if ( gg >= 10000 ) break;
                                os << a << ", " << b << ", " 
                                    << c << ", " << d << ", " 
                                    << e << ", " << f << ", " 
                                    << g << "\n";
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
std::流操作系统(“listOfCombinations.txt”);
使用std::pow;
对于(双a=0;10000)中断;
对于(双b=0;b 10000)中断;
对于(双c=0;c 10000)断裂;
//下一行为用户提供了一些视觉反馈
//用户了解进度(打印当前a、b和c
//价值观)。

Std::CUT< P>因为您也要求用不同的语言解决问题,这里是C++中的一个快速和肮脏的程序,也包含了@ TMykLeBu的建议。
#include <iostream>
#include <fstream>
#include <cmath>

int main()
{
    std::ofstream os( "listOfCombinations.txt" );
    using std::pow;
    for( double a = 0; a <= 65; ++a ) {
        double aa = (pow(1.15, a) - 1);
        if ( aa > 10000 ) break;
        for( double b = 0; b <= 52; ++b ) {
            double bb = aa + (20/3) * (pow(1.15, b) - 1);
            if ( bb > 10000 ) break;
            for( double c = 0; c <= 40; ++c ) {
                double cc = bb + (100/3) * (pow(1.15, c) - 1);
                if ( cc > 10000 ) break;
                // The following line provides some visual feedback for the
                // user about the progress (it prints current a, b, and c
                // values).
                std::cout << a << "   " << b << "   " << c << std::endl;
                for( double d = 0; d <= 28; ++d ) {
                    double dd = cc + 200 * ( pow(1.15, d) - 1);
                    if ( dd > 10000 ) break;
                    for( double e = 0; e <= 19; ++e ) {
                        double ee = dd + (2000/3) * (pow(1.15, e) - 1);
                        if ( ee > 10000 ) break;
                        for( double f = 0; f <= 11; ++f ) {
                            double ff = ee + (8000/3) * (pow(1.15, f) - 1);
                            if ( ff > 10000 ) break;
                            for( double g = 0; g <= 4; ++g ) {
                                double gg = ff + (40000/3) * (pow(1.15, g) - 1);
                                if ( gg >= 10000 ) break;
                                os << a << ", " << b << ", " 
                                    << c << ", " << d << ", " 
                                    << e << ", " << f << ", " 
                                    << g << "\n";
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}
#包括
#包括
#包括
int main()
{
std::流操作系统(“listOfCombinations.txt”);
使用std::pow;
双倍
local res={
    {1,2,3,4,5,6,7},
    {8,9,10,11,12,13,14}
}

for k,v in ipairs(res) do
    -- either concatenate each line and produce a huge string
    res[k]=table.concat(v,", ")
  -- or write each line to a file in this loop
end

local text=table.concat(res,"\n")
print(text)
1, 2, 3, 4, 5, 6, 7
8, 9, 10, 11, 12, 13, 14