A program that writes code: should it use 'string'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramon F Herrera

    A program that writes code: should it use 'string'?


    I am writing a program that generates source code. See a snippet
    below. My question is about the use of that growing 'code' variable.
    Is it efficient? Is is recommended for this case?

    The code generated can grow a lot. Perhaps I should allocate a large
    max size in advance?

    TIA,

    -RFH


    -------------

    void SynthesizeTextF ield(CompleteFi eld fullTextField)
    {
    string code;
    string baseFieldname = "text";
    stringstream ss;
    static int subindex = 1;

    code = "Field ";
    code += baseFieldname;
    ss << subindex;
    code += ss.str();
    code += " ";
    code += "doc.FieldCreat e(\"";
    code += baseFieldname;
    code += ss.str();
    code += "\", Field::e_text, \"\", \"\");";

    subindex++;
    }


  • Kai-Uwe Bux

    #2
    Re: A program that writes code: should it use 'string'?

    Ramon F Herrera wrote:
    >
    I am writing a program that generates source code. See a snippet
    below. My question is about the use of that growing 'code' variable.
    Is it efficient? Is is recommended for this case?

    Recommended is to measure before you optimize. Write the program so that it
    is easy to understand. When (and only when) you have a performance problem,
    don't guess what the cause might be; instead, use a profiler to identify
    the bottleneck and then do something about it.

    The code generated can grow a lot. Perhaps I should allocate a large
    max size in advance?
    [snip]

    If profiling shows that appending to the string is too costly, reserving a
    certain capacity would be the first thing to try. It's the least intrusive
    measure.


    Best

    Kai-Uwe Bux

    Comment

    • Daniel T.

      #3
      Re: A program that writes code: should it use 'string'?

      Ramon F Herrera <ramon@conexus. netwrote:
      I am writing a program that generates source code. See a snippet
      below. My question is about the use of that growing 'code' variable.
      Is it efficient? Is is recommended for this case?
      >
      The code generated can grow a lot. Perhaps I should allocate a large
      max size in advance?
      A std::string is just a vector<charwith some extra functions that you
      probably don't need for this particular variable.

      I think your best bet would be to make your own class to represent
      "code", implement that class with a string if that makes sense to you.
      The nice thing is you can always change the implementation of the class
      later as profiling requires, without affecting any other code.

      Comment

      • James Kanze

        #4
        Re: A program that writes code: should it use 'string'?

        On Jun 1, 11:58 pm, Ramon F Herrera <ra...@conexus. netwrote:
        I am writing a program that generates source code. See a snippet
        below. My question is about the use of that growing 'code' variable.
        Is it efficient? Is is recommended for this case?
        The code generated can grow a lot. Perhaps I should allocate a large
        max size in advance?
        -------------
        void SynthesizeTextF ield(CompleteFi eld fullTextField)
        {
        string code;
        string baseFieldname = "text";
        stringstream ss;
        static int subindex = 1;
        >
        code = "Field ";
        code += baseFieldname;
        ss << subindex;
        code += ss.str();
        code += " ";
        code += "doc.FieldCreat e(\"";
        code += baseFieldname;
        code += ss.str();
        code += "\", Field::e_text, \"\", \"\");";
        >
        subindex++;
        }
        For starters, I'd generate (or support generation) directly into
        the output stream. Something like:

        std::ostream&
        SynthesizeTextF ield(
        std::ostream& dest,
        ... )
        {
        // ...
        return dest ;
        }

        You're formatting here (some of the data is numeric,
        apparently), so you might as well treat the entire thing as a
        stream. And you'll certainly be outputting it in the end;
        there's not much you can do with C++ source code within the
        program, so you might as well generate directly into the output
        stream, and never build the string at all.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Juha Nieminen

          #5
          Re: A program that writes code: should it use 'string'?

          Ramon F Herrera wrote:
          I am writing a program that generates source code. See a snippet
          below. My question is about the use of that growing 'code' variable.
          Is it efficient? Is is recommended for this case?
          I think it's ok. You could also say something like
          "code.reserve(1 00*1024);" which allocates 100kB (or any other
          amount you feel is about correct) of memory for it so that it
          never has to resize (unless you exceed that limit, of course),
          which might make it slightly more efficient.

          Comment

          Working...