Bibi
Bibi

Reputation: 53

How to (effienciently) construct a string in Cython?

I am trying to fasten my python code with cython.

In cython, one of the bottlenecks I try to remove is the construction of a filename (string) in a loop. However, I don't manage to re write my python code in a cython way:

 cdef str filename, path

 for ii in range(len(a0)):
   for jj in range(len(a1)):
       filename = self.path + 'directory' + format(int(lon[ii,jj]),'02d')+ '_' + format(int(lat[ii,jj]),'02d') + '.csv'

Any help on how to rewrite this would be much appreciated! Thanks

Upvotes: 0

Views: 720

Answers (1)

hpaulj
hpaulj

Reputation: 231385

I suspect you can speed up the formatting in Python. For example:

In [67]: lat=np.arange(6).reshape(2,3)
In [68]: names=[]
In [69]: for i in range(lat.shape[0]):
    ...:     for j in range(lat.shape[1]):
    ...:        name = 'path/'+'directory'+format(lat[i,j],'02d')+'_'+format(lat
    ...: [i,j]+3,'02d')+'.csv'
    ...:        names.append(name)
    ...:        
In [70]: names
Out[70]: 
['path/directory00_03.csv',
 'path/directory01_04.csv',
 'path/directory02_05.csv',
 'path/directory03_06.csv',
 'path/directory04_07.csv',
 'path/directory05_08.csv']

can be reworked as:

In [71]: names=[]
In [72]: fmt='path/'+'directory/'+'%02d_%02d.csv'
In [73]: for i in range(lat.shape[0]):
    ...:     for j in range(lat.shape[1]):
    ...:        name = fmt%(lat[i,j],lat[i,j])
    ...:        names.append(name)

In other words, create one format string at the start, and use that repeatedly.

Or for the PY3 styles format:

fmt = 'path/directory{:02d}_{:02d}.csv'
name = fmt.format(lat[i,j],lat[i,j])

Quick time tests suggest a 2-3 x speedup (more with the older % style).

Upvotes: 0

Related Questions