0

I did some profiling for a program I wrote and discovered that ogr's layer.GetFeatureCount() is significantly slower than just using python's BIF len().

Here are my test results

def test():
        import ogr
        b_path = r'/../...'
        bounds =  r'%s/bounds' % b_path
        province =  glob.glob(r'%s/*.shp' % (bounds,))[0]
        driver = ogr.GetDriverByName('ESRI Shapefile')
        admin_shp = driver.Open(province,0)
        l = admin_shp.GetLayer(0)
        lc = l.GetFeatureCount() # first round
        lc = len(l)              # second round



if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test()", setup="from __main__ import test"))

first round

228.255430937

second round

124.03532505

Can somebody explain why this is?

3

I tried this example and I got different results. For a shapefile containing a layer with 246 features, the running time was 304usec per loop on GetFeatureCount(), while it was 315usec per loop on len(layer). Timeit used 1000 loops to evaluate this.

This was done using:

testFeatureCount.py

import osgeo.ogr
def testFeature():
    shapefile = osgeo.ogr.Open("TM_WORLD_BORDERS-0.2.shp")
    layer = shapefile.GetLayer(0)
    lc = layer.GetFeatureCount()
    print "Number of features: ", lc

def testLen():
    shapefile = osgeo.ogr.Open("TM_WORLD_BORDERS-0.2.shp")
    layer = shapefile.GetLayer(0)
    lc = len(layer)
    print "Number of features: ", lc

You can find the TM_WORLD_BORDERS-0.2.shp file here!

python -mtimeit -s'import testFeatureCount' 'testFeatureCount.testFeature()'

python -mtimeit -s'import testFeatureCount' 'testFeatureCount.testLen()'

To me it looks like you ran the first run through with both of your lines enabled, as it pretty much takes around double the amount of time. When I run my example without the print statement they both take exactly the same amount of time.

  • This is strange. I didn't even include the second line in the first run. I will do a second test on monday. Thanks already. anyway. – LarsVegas Aug 30 '13 at 17:26
  • 1
    To do a better, more correct test, you should remove all the osgeo.ogr.Open and GetLayer logic out so it's just the count itself that is timed. – Nathan W Aug 30 '13 at 23:01
  • I did some more testing and I can't reproduce the results from my first test. In the end I must have had both lines in my function (even though I still don't want to believe it!). Thanks for testing along. – LarsVegas Sep 2 '13 at 8:20
3
  • len() is a pure Python function that return the length (the number of items) of an object (whatever the object) -> in this case the number of features.
  • .GetFeatureCount() did the same thing (number of features in the layer) but has some additional functions compared to the simple len(). It is based on the CPP OGRLayer::GetFeatureCount() function (in the file _ogr.so)

Read the comments of

  def GetFeatureCount(self, *args, **kwargs):

in the ogr.py file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.