Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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
Java 两个for循环,内部for循环增加1000倍,而速度仅增加100倍?_Java_Algorithm_Complexity Theory - Fatal编程技术网

Java 两个for循环,内部for循环增加1000倍,而速度仅增加100倍?

Java 两个for循环,内部for循环增加1000倍,而速度仅增加100倍?,java,algorithm,complexity-theory,Java,Algorithm,Complexity Theory,我正在分析算法的计算复杂度,我有两个for循环 short i=0; short j=0; short ii=0; short[] counts = new short[2]; int label_size = MapRedUtils.label_size; int max_index=0; int sample_size =data.getSampleSize(); float

我正在分析算法的计算复杂度,我有两个for循环

    short i=0;
    short j=0;
    short ii=0;
    short[] counts = new short[2];

    int label_size  = MapRedUtils.label_size;
    int max_index=0;                        
    int sample_size =data.getSampleSize();

    float max_ig = 0;
    float totalVar=0;
    float var=0;
    float cov_ig;

    float[] tocopy = new float[label_size];        
    float[][] sums = new float[2][];
    float[] totalSum = new float[label_size];



    byte value;
    ByteBuffer label = ByteBuffer.allocate(label_size*4); 

    for( j=0; j<2; j++ )        
        sums[j] = new float[label_size];

    for( ii=0; ii<3; ii++)// 3 ways of split the current node
    {          
          long startTime;
    long endTime;
    long totalTime;
    startTime = System.currentTimeMillis();
        counts[0]=0;
        counts[1]=0;

        System.arraycopy(tocopy,0,totalSum,0,label_size);
        System.arraycopy(tocopy,0,sums[0],0,label_size);
        System.arraycopy(tocopy,0,sums[1],0,label_size);

        for ( i = 0; i < sample_size; i++) 
        {
            OneSample instance = data.get(i);
            value = (byte) instance.getTheGenoytpe(snpid);

            label = instance.getPhenotype();                                                                          

            if( value==ii)
            {   
                counts[0]++;
                for(j=0; j< label_size; j++)
                     sums[0][j] += label.getFloat(j*4);
            }
            else
            {
                counts[1]++;                    
                for(j=0; j< label_size; j++)
                    sums[1][j] += label.getFloat(j*4);                    
            }

            for(j=0; j< label_size; j++)  
                totalSum[j] += label.getFloat(j*4);
        }                                              

          totalVar=0;
          var=0;
         for(i=0; i< label_size; i++)
         {
           totalVar += totalSum[i]*totalSum[i];          
         } 

        totalVar = totalVar/sample_size;//it is averaged by sample size

        for(j=0; j< 2; j++)
           //var += sumSquared(sums[j],  MapRedUtils.inverse_covariance , counts[j]);  
             var += sumSquared(sums[j], counts[j]);


        cov_ig = var- totalVar;

        if(cov_ig > max_ig)
        {
            max_ig=cov_ig;
            max_index=ii;
        } 
        endTime = System.currentTimeMillis();                    
        totalTime = (endTime - startTime);
        System.out.println(totalTime);
short i=0;
短j=0;
短ii=0;
短[]计数=新短[2];
int label_size=MapRedUtils.label_size;
int max_指数=0;
int sample_size=data.getSampleSize();
浮动最大值=0;
float totalVar=0;
浮动var=0;
浮冠免疫球蛋白;
float[]tocopy=新的float[标签大小];
浮动[]总和=新浮动[2][];
float[]totalSum=新的float[标签大小];
字节值;
ByteBuffer标签=ByteBuffer.allocate(标签大小*4);

对于(j=0;j当label=1时,外部循环的大部分时间用于“在这里做点什么”和设置内部循环,因为只运行一次循环“在这里也做点什么”只占工作的一小部分。假设“在这里做点什么”和设置内部循环需要100个时间单位和“在这里也做点什么”只需要10个时间单位。程序的总运行时间为110*sample\u size。现在您将label增加到1000.100+10*1000=10100。因此总运行时间为10100*sample\u size.10100/110=91.8。因为“在这里做点什么”花了一段时间,它大大减少了增加标签的影响。你必须考虑“在这里做某事”的比例,“在这里也做一些事情”,而不仅仅是旧标签值与新标签值的比值。

什么语言?(那个示例伪代码没有我知道的语言中的嵌套循环)。理论上,理论和实践是一样的。你能展示一些真实的代码吗?我使用java谢谢我现在展示真实的代码分析器在这里很有价值,所以你可以看到时间在哪里消耗。听说过吗?