How can I optimize the code to parse lines in a file? I have a working importer that needs to handle large data sets ie. thousands of primitives.

When I import a file containing 2500 objects it takes roughly 90 seconds to import. Now if I import the same scene through the obj importer it takes about 10 -15 seconds.

What can I do to optimize this? Here is part of the code below:

    #read each line of the .ssynth file
for line in file:
    if  objCount % 25000 == 0:
        print ("Import progress report: " + str(objCount) + " objects")
        args = line.split()
        argsIndex = 0
        colR = 1
        colB = 1
        colG = 1

    #begin sphere shape. here we import any spheres
    if args[argsIndex] == "s":
        argsIndex += 1
        cx = float(args[argsIndex])
        argsIndex += 1
        cy = float(args[argsIndex])
        argsIndex += 1
        cz = float(args[argsIndex])
        argsIndex += 1
        radius = float(args[argsIndex])
        argsIndex += 1

        colR = float(args[argsIndex])
        argsIndex += 1
        colG = float(args[argsIndex])
        argsIndex += 1
        colB = float(args[argsIndex])
        argsIndex += 1

        # Set the Alpha - maybe we can use this later
        alpha = 1.0

        meshSphere =  bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=2, size=radius, location=([cx, cy, cz,]))

        #convert rgb values to hex for group naming
        def rgb_to_hex(rgb):
            return '#%02x%02x%02x' % rgb

        # Let's set the Object Color
        bpy.context.object.color = (colR, colG, colB, alpha)
        # Name for grouping
        matName = rgb_to_hex((colR,colG,colB))
        # Add a material slot to each object
        bpy.ops.object.material_slot_add()
        # Find the active material
        bpy.context.object.active_material_index = 0
        # Add that material to the slot of each object
        bpy.context.object.active_material = bpy.data.materials["SSmat"]
Suggest not adding a new mesh for every object,
bpy.ops.mesh.primitive_ico_sphere_add

Instead you can add a sphere once, then reuse the mesh between all objects changing their scale. Its also possible for each object to have their own material so you can still share object data but have different colors.

Though you could even make do with a single material for all and just use object color.