AddData2f, addData3f perfomance issue.

I have following code blocks in my game:

        for tileY in self.bigAxisRangeX:
            for tileX in self.bigAxisRangeY:                                                                            
                for y in self.smallAxisRange:
                    for x in self.smallAxisRange:
                        vertex.addData3f(self.X+tileX+x, self.Y+tileY+y, self.vertices[tileY+y][tileX+x].height)

and

               
               for i in self.fourRange:                                       
                    tilesetID = 0
                    tileID = 0
                    if i < len(tilesetList):
                        tilesetID = tilesetList[i]
                        tileID = tileIDList[i]                           
                                            
                    tileset = self.tc.tilesets[tilesetID]
                                                                
                    tX =  tileID/ 4
                    tY =  3 - (tileID % 4)                
                    
                    left = (tileset.tcX+tX*tileset.tileWidth)/float(self.tc.width)                   
                    right = (tileset.tcX+(tX+1)*tileset.tileWidth)/float(self.tc.width)                   
                    
                    top =  (self.tc.height-tileset.tcY-tileset.height + (tY+1)*tileset.tileHeight)/float(self.tc.height)
                    bottom = (self.tc.height-tileset.tcY-tileset.height + tY*tileset.tileHeight)/float(self.tc.height)
                    
                    texcoord[i].addData2f(left, bottom)        
                    texcoord[i].addData2f(right, bottom)  
                    texcoord[i].addData2f(left,top)    
                    texcoord[i].addData2f(right, top)  
 

As profiler shows, most tame takes methods addData2f and addData3f. Is there any way to optimize performance of custom geometry creation? Right now to create tile map 124x124 takes 16 seconds. ~90% of time is taken by methods addData2f and addData3f.

I think the loops is what is most intensive for python.
Python does loops very slow.
I also use code like yours to generate terrain, but how I solve it: I write the code in C++, compile it into a shared library, and use it in python.

Actually don’t know how I can use my Python classes as Tileset, TilesetCollection etc. from C++ library. So I prefer to make prototype in pure Python. But so low performance scary me =( I know that moving critical for performance code to C++ will help, but is there any other ways?

Maybe you can build your geometry up once, and then save it to a bam file using nodePath.writeBamFile()? On subsequent runs, you can simply load up the bam file.

David

No, i can’t load this graphic once just because i should create random landscape on every editor startup. Btw here is the question, why you think that bam loader will add vertex data to vertexbuffer faster then just addDataXf? Is it add vertex data in some other way then just addDataXf? If yes, can i use same methods in my code to simulate bam loading?

The bam loader will be faster because the bam loader is not written in Python.

It’s not the addDataX calls themselves that are slow; it’s the overhead of calling them from Python. Python is an excellent language for doing the high-level manipulation of your scene, like moving objects around; but it’s not so great at doing things on a per-vertex level, simply because the overhead of a single Python function call tends to dominate when you end up calling the same function repeatedly many thousands of times.

Your two choices for improving performance are (a) moving the thousands of function calls into C++, or (b) not making thousands of function calls.

David