首页 \ 问答 \ python for循环每次迭代都会变慢(python for-loop slower each iteration)

python for循环每次迭代都会变慢(python for-loop slower each iteration)

我正在尝试优化一些python代码(以加快一些矩阵操作),我的代码类似于这个(我的真实数据集也类似于'gps'),

import numpy as np
gps = [np.random.rand(50,50) for i in xrange(1000)]
ips = np.zeros( (len(gps),len(gps)), dtype='float32')

for i in xrange(len(gps)):
  for j in xrange(0,i+1):
    ips[i,j]= f.innerProd(gps[i],gps[j])
    ips[j,i]= ips[i,j]
   print "Inner product matrix: %3.0f %% done (%d of %d)"%  \
               (((i+1)**2.)/(len(gps)**2.)*100, i, len(gps))

def innerProd(mat1,mat2):
    return float(np.sum(np.dot(np.dot(mat1,mat2),mat1)))

我想要理解的是,为什么程序在第一次迭代期间开始快速运行,然后在进一步迭代时减慢? 我知道这个问题可能有点天真但我真的想在我尝试其他任何事情之前更清楚地了解发生了什么。 我已经在Fortran中实现了我的函数(在Fortran域中留下任何for循环)并使用f2py创建一个动态lib来从python调用函数,这将是python中的新代码。

import numpy as np
import myfortranInnProd as fip

gps = [np.random.rand(50,50) for i in xrange(1000)]
ips = np.zeros( (len(gps),len(gps)), dtype='float32')

ips = fip.innerProd(gps)

不幸的是,我发现(令人惊讶的是)我的fortran-python版本运行速度比第一个版本慢1.5~2倍(重要的是要提到我在Fortran实现上使用了MATMUL())。 我一直在谷歌搜索一段时间,我相信这种“减速”与内存带宽,内存分配或缓存有关,考虑到大型数据集,但我不太确定背后真正发生的事情以及如何我可以改善表现吗? 我已经在小型英特尔原子,2GB内存和4核英特尔至强处理器上运行代码,8GB(当然还有相应的缩放数据集)并且“减速”行为是相同的。

我只需要理解为什么这种“减速”会发生? 如果我在C中实现该功能,它会有用吗? 或尝试实现它在GPU上运行? 还有其他想法如何改进吗? 提前致谢


I am trying to optimize some python code (to speed up some matrix operations), my code is something similar to this one (my real dataset is also similar to 'gps'),

import numpy as np
gps = [np.random.rand(50,50) for i in xrange(1000)]
ips = np.zeros( (len(gps),len(gps)), dtype='float32')

for i in xrange(len(gps)):
  for j in xrange(0,i+1):
    ips[i,j]= f.innerProd(gps[i],gps[j])
    ips[j,i]= ips[i,j]
   print "Inner product matrix: %3.0f %% done (%d of %d)"%  \
               (((i+1)**2.)/(len(gps)**2.)*100, i, len(gps))

def innerProd(mat1,mat2):
    return float(np.sum(np.dot(np.dot(mat1,mat2),mat1)))

What I would like to understand is , why is it that the program begins running fast during the first iterations and then slows down as it iterates further? I know the question might be a bit naive but I really want to have a clearer idea of what is happening before I attempt anything else. I already implemented my function in Fortran (leaving within the Fortran realm any for loops) and used f2py to create a dynamic lib to call the function from python, this would be the new code in python..

import numpy as np
import myfortranInnProd as fip

gps = [np.random.rand(50,50) for i in xrange(1000)]
ips = np.zeros( (len(gps),len(gps)), dtype='float32')

ips = fip.innerProd(gps)

unfortunately I only found out (surprisingly) that my fortran-python version runs 1.5 ~ 2 times slower than the first version (it is important to mention that I used MATMUL() on the Fortran implementation). I have been googling around for a while and I believe that this "slow down" has something to do with the memory bandwidth, memory allocation or caching, given the large datasets, but I am not very sure about what is really happening behind and how could I improve the performance. I have run the code on both a small intel atom , 2GB ram and a 4 core intel xeon, with 8GB (of course with a correspondingly scaled dataset) and the "slow down" behavior is the same.

I just need to understand why is it that this 'slow down' happens? would it do any good if i implement the function in C ? or try to implement it to run on a GPU ? Any other ideas how to improve it? Thanks in advance


原文:https://stackoverflow.com/questions/5845826
更新时间:2022-09-03 09:09

最满意答案

我这样做了:

AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    // Prepare the Interstitial Ad
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    interstitial.loadAd(adRequest);

    interstitial.setAdListener(new AdListener() {
        public void onAdLoaded() {
            displayInterstitial();
        }
        public void onAdClosed() {
            requestNewInterstitial();
        }

    });
}

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

public void requestNewInterstitial() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            AdRequest adRequest = new AdRequest.Builder()
                    .build();
            interstitial.loadAd(adRequest);

            if (mHandler != null) {
                mHandler.postDelayed(this, 100000); //time (ms)
            }
        }
    }, 100000); //time (ms)
}

I did it this way :

AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    // Prepare the Interstitial Ad
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    interstitial.loadAd(adRequest);

    interstitial.setAdListener(new AdListener() {
        public void onAdLoaded() {
            displayInterstitial();
        }
        public void onAdClosed() {
            requestNewInterstitial();
        }

    });
}

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

public void requestNewInterstitial() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            AdRequest adRequest = new AdRequest.Builder()
                    .build();
            interstitial.loadAd(adRequest);

            if (mHandler != null) {
                mHandler.postDelayed(this, 100000); //time (ms)
            }
        }
    }, 100000); //time (ms)
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)