Optimize code generated by sympy

Here is my own small script which is based on asmeurers hint:

def sympyToC( symname, symfunc ):
    tmpsyms = numbered_symbols("tmp")
    symbols, simple = cse(symfunc, symbols=tmpsyms)
    symbolslist = map(lambda x:str(x), list(symfunc.atoms(Symbol)) )
    symbolslist.sort()
    varstring=",".join( " double "+x for x in symbolslist )

    c_code = "double "+str(symname)+"("+varstring+" )\n"
    c_code +=  "{\n"
    for s in symbols:
        #print s
        c_code +=  "  double " +ccode(s[0]) + " = " + ccode(s[1]) + ";\n"
    c_code +=  "  r = " + ccode(simple[0])+";\n"
    c_code +=  "  return r;\n"
    c_code += "}\n"
    return c_code

And for python3.5+:

def sympyToC( symname, symfunc ):
    tmpsyms = numbered_symbols("tmp")
    symbols, simple = cse(symfunc, symbols=tmpsyms)
    symbolslist = sorted(map(lambda x:str(x), list(symfunc.atoms(Symbol))))
    varstring=",".join( " double "+x for x in symbolslist )
    c_code = "double "+str(symname)+"("+varstring+" )\n"
    c_code +=  "{\n"
    for s in symbols:
        #print s
        c_code +=  "  double " +ccode(s[0]) + " = " + ccode(s[1]) + ";\n"
    c_code +=  "  r = " + ccode(simple[0])+";\n"
    c_code +=  "  return r;\n"
    c_code += "}\n"
    return c_code

gcc will probably optimize this, but if you want to do it yourself, take a look at cse. http://docs.sympy.org/latest/modules/simplify/simplify.html#module-sympy.simplify.cse_main