From: Robert Feldt Date: 2000-12-28T21:22:50+09:00 Subject: [ruby-talk:8170] Speeding up string appends Hi, I need to convert large strings to valid C char arrays. A naive way goes through the string while appending "#{str[i]}," to an output string but this (of course) doesn't scale well. I tried: # To speed up appends... class AppendString def initialize(size) @str = " " * size @len = 0 end def <<(astr) al = astr.length @str[@len, al] = astr @len += al end def to_s @str[0,@len] end end # Convert string to C-readable char array. def string_to_c_char_array(str, arrayname = nil) s = AppendString.new(4*str.length) # Max 4 bytes per char s << "const char #{arrayname}[#{str.length}] = {" if arrayname for i in 0...(str.length-1) s << "#{str[i]}," end s << "#{str[str.length-1]}" s << "};" if arrayname s.to_s end but it still takes a long time (about 40 kB/s on my 366 Mhz laptop). Is there a faster/better way? Sorry if this has been discussed in any of the previous speed-up threads; I haven't been following them. Regards, Robert