English 中文(简体)
可可——nes虫的最大深度?
原标题:Cocoa - maximum depth for nested loops?

我正试图书写一种算法,确定从10x10电网中选择10个数值的可能解决办法。 两个数值不能相同。 共有10万个组合(超过3 600 000个)。

我最初的算法使用10套nes,简单地检查了10平方米的可能组合。 当我试图在我的MacBook上接手时,需要许多分钟的时间才能免除每只 log一只 log子对ole子的检测,这样我就可以看看这些检测。

问题在于,该表一直持续到测试编号714271,然后冻结。 这一结果可以重复。

我认为,它是一种记忆,有些反面的东西超出了其最大允许的价值,但从我的角度来说,这个数字没有任何意义。


法典认为:

-(IBAction)findSolutions:(id)sender{

    NSMutableArray* flags = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], [NSNumber numberWithInt:0], nil];

    NSMutableString* solutionsString = [[NSMutableString alloc]init];

    int a,b,c,d,e,f,g,h,i,j,z,sum;

    for(a=0;a<=9;a++){
        for(b=0;b<=9;b++){
            for(c=0;c<=9;c++){
                for(d=0;d<=9;d++){
                    for(e=0;e<=9;e++){
                        for(f=0;f<=9;f++){
                            for(g=0;g<=9;g++){
                                for(h=0;h<=9;h++){
                                    for(i=0;i<=9;i++){
                                        for(j=0;j<=9;j++){
                                            for(z=0;z<=9;z++){
                                                [flags replaceObjectAtIndex:z withObject:[NSNumber numberWithInt:0]];
                                            } //Clear the flags matrix

                                            //Load the flags matrix
                                            [flags replaceObjectAtIndex:a withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:b withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:c withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:d withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:e withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:f withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:g withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:h withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
                                            [flags replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:1]];

                                            sum = 0;
                                            for(z=0;z<=9;z++){
                                                sum = sum + [[flags objectAtIndex:z]intValue];
                                            } // sum the flags matrix. Will = 10 if all columns are filled

                                            if (sum == 10) {
                                                NSLog(@"Test no. %d, sum=%d, a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",y,sum,a,b,c,d,e,f,g,h,i,j);
                                                [solutionsString appendString:[NSString stringWithFormat:@"Test no. %d, sum=%d, a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",y,sum,a,b,c,d,e,f,g,h,i,j]];
                                                [txtSolutionsFound setStringValue:solutionsString];

                                            } // These are possible solutions

                                            NSLog(@"a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d",a,b,c,d,e,f,g,h,i,j);

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 }

FINAL UPDATE I have a confession to make. I gave up trying to get the code to work in obj-c and rewrote it in Python. It has now been running for a couple of hours, checked 1.2 billion of the 10^10 combinations, has not clagged up the system memory and on average is using 50% less CPU time than the obj-c code. I love the look of Cocoa apps and the creation of the UI is brilliant but for sheer workability, Python is hard to beat.

灰色成像:

row0 = [164,116,106,76,122,46,109,86,7,134]
row1 = [70,170,108,25,182,77,134,192,60,26]
row2 = [14,108,140,130,90,46,6,154,168,90]
row3 = [138,48,130,127,54,2,112,78,76,98]
row4 = [86,65,110,22,64,82,46,62,146,94]
row5 = [70,194,20,152,76,162,54,98,122,50]
row6 = [91,0,116,230,32,138,203,175,104,88]
row7 = [68,222,87,124,99,209,28,147,108,72]
row8 = [51,85,74,40,132,98,118,159,70,44]
row9 = [30,122,92,190,8,77,152,7,106,70]

maxvalue = 0

flagmatrix = [0,0,0,0,0,0,0,0,0,0]
solutionmatrix = [0,0,0,0,0,0,0,0,0,0]

for a in range(0,10):
    for b in range(0,10):
        for c in range(0,10):
            for d in range(0,10):
                for e in range(0,10):
                    for f in range(0,10):
                        for g in range(0,10):
                            for h in range(0,10):
                                for i in range(0,10):
                                    for j in range(0,10):
                                        for z in range(0,10):
                                            flagmatrix[z]=0
                                            #This will clear the flag matrix

                                        #Now load the matrix
                                        flagmatrix[a]=1
                                        flagmatrix[b]=1
                                        flagmatrix[c]=1
                                        flagmatrix[d]=1
                                        flagmatrix[e]=1
                                        flagmatrix[f]=1
                                        flagmatrix[g]=1
                                        flagmatrix[h]=1
                                        flagmatrix[i]=1
                                        flagmatrix[j]=1

                                        sum=0
                                        for flag in flagmatrix:
                                            sum = sum+flag
                                        if sum == 10:
                                             print "solution found at ",a,b,c,d,e,f,g,h,i,j
                                            solution = row0[a]+row1[b]+row2[c]+row3[d]+row4[e]+row5[f]+row6[g]+row7[h]+row8[i]+row9[j]
                                            print "Solution = ", solution
                                            if solution > maxvalue:
                                                maxvalue = solution
                                                solutionmatrix[1]=b
                                                solutionmatrix[2]=c
                                                solutionmatrix[3]=d
                                                solutionmatrix[4]=e
                                                solutionmatrix[5]=f
                                                solutionmatrix[6]=g
                                                solutionmatrix[7]=h
                                                solutionmatrix[8]=i
                                                solutionmatrix[9]=j


                                            print "Current maxvalue = ", maxvalue
print "Final max value = ", maxvalue
print "Final solution matrix = ", solutionmatrix
最佳回答

我遵循你的法典,并在几分钟内开始穿透。

现在,这是你方案的一个指挥系统版本。 我已取消了每次测试的所有设立临时安保分遣队和安保记录仪的工作,并且尚未通过国家安保记录仪进行成功测试。 这本版本的唯一目标就是NSNumbers,正如我在关于enISign的评论中提到的那样 回答时,由于你只制造了其中两件,NSNumber客物体被拦截,就在你看来制造了NSNumber客体时,你实际上只能重新使用以前制造的物体。

因此,看到你的方案消耗了一张记忆灯。

贵方案消费传记的下一个步骤是使用莱卡斯模板,根据仪器操作。 这是我所做的事。 我每隔几秒钟就进行一次测验,文书每次都报告了5至12兆赫的记忆增长。

看一看一幅速记(说明自上一次传闻以来所分配的内容),我发现,这是所有不客观的记忆。 看一看一个拨款的批量,我发现,这笔拨款由......编码<>autorelease/code>分配,显然直接取自main

<代码>main上的双轨制 它把我带上了一名NSNumber物体的“生机”。 因此,我推断,虽然它重复使用现有物体,但它也保留和自动释放这些物体。

如文件所示,物体对<代码>autorelease作出回应,在自动释放池内添加。 之所以如此重要,是因为自动取款库的核心基本上是一个可变阵列(当它耗尽时释放了全部内容)。 通常,这的确算得很多,因为全部物体的面积远远大于其流离失所,因此,在座,但就你而言,你有两个目标,在方案实施期间,每加数百万次。

解决办法是将守则中的“创造”表述转化为可变的声明:

NSNumber *zero = [NSNumber numberWithInt:0];
NSNumber *one = [NSNumber numberWithInt:1];

这些变量以前用变数出现在各地,取而代之。 该方案仍然缓慢,但记忆的使用现在比较平常。

我不知道这是否与你的问题有关,但也许会是,而且无论如何,它会带来必要的改善。

此外,NS Carlo在系统记录上写上,该记录可到磁盘上。 考虑改用NSTextView,或甚至将电网及其标志位置引向习俗。 你们不得不打破该法典,不单单是长期居住的地方,而这是又一个必要的改进,需要了解什么东西,这是一个非常好的做法。

问题回答

你们眼前的问题与 lo、基本可可记忆管理的最大深度无关。 你们创造,然后在目前的“自动释放计划”中积累数百万件物品。

For further info try http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

在类似情况下,使用NSNumber而不使用固定装置的间接费用相当可观。 最好在C部分重写业绩判断(如果坚持这一算法)。

That s not to say you can t use Objective-C for this, you just have to understand the lifecycle of objects you create and not use up all available memory with what you intended to be temporary variables.

www.un.org/Depts/DGACM/index_spanish.htm 要求放弃:算法不是好的算法,应当加以改进,一般不应依赖语言特征,使算法不好。 这就是说:

之所以能更好地使用你的代码,是因为它正在使用garbage Collection。 在记忆使用达到一定限度后,Sharma系统看着已经分配的记忆,以及你们的法典不再需要的再使用记忆的释放。

页: 1 在你的案件中,记忆被标为“自动释放”,这意味着在你目前处理事件的工作完成时,将释放(没有任何其他明确编码)用于重新使用;在你的法典中,整整整座圆是一种单一的“空白”(技术术语;-),因此,不会释放作为“自动释放”的记忆。

现任目标C X(但不是SOS)支持垃圾收集。 进入您的项目环境,寻找“客观-碳垃圾收集”,将其“支持”。 现在,它应当像灰色版那样做,或者可能做得更好......。

Ironically your algorithm, non-optimal as it is, does not appear to actually allocate a lot of memory; as others have pointed out NSNumber should only allocate one instance per actual numeric value. The poor memory-performance appears to be a side-effect of the way "autorelease" is working. Garbage collection should not suffer from that side-effect and so your code will probably run in a lot less memory and do little, if any, garbage collection.

This is much related to the N-Queens-Problem (which also checks diagonals, so it will be more a N-Rook-Problem).

无论如何,这种繁重的工作在主线上都不应去做(这将使申请没有反应)。 担任这一职务的有背景工作者(或几个人,或 GCD)。

令人振奋的是,可能有许多事情有待改进。 首先要想的是,一旦安装,就立即检查每一个新项目,以便你根本不必做内 lo。

在技术方面,我知道的<<>t>fors”的数量不受限制,尽管其他代码结构可能比较容易。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签