[Twisted-Python] transport.write(a + b) versus [transport.write(a); transport.write(b)]

Itamar Turner-Trauring itamar at itamarst.org
Wed Feb 16 06:05:29 MST 2011


On Wed, 2011-02-16 at 12:00 +0000, Carlos Valiente wrote:
> From twisted.protocols.basic.IntNStringReceiver.sendString():
> 
>  def sendString(self, string):
>      self.transport.write(
>          struct.pack(self.structFormat, len(string)) + string)
> 
> Would it make sense to do something like this instead:
> 
>  def sendString(self, string):
>      self.transport.write(
>          struct.pack(self.structFormat, len(string)))
>      self.transport.write(string)
> 
> in order to avoid the creation of the extra string object?

Maybe it would be faster, maybe it wouldn't; instead of creating a new
string object, you're doing another function call. There is a
transport.writeSequence() method that takes a list, which can provide a
speed up, but more likely in cases where you have lots of small strings
rather than just two.

Some questions you might want to be asking:

1. Is your application fast enough? If it is, you're done.
2. Where are the performance bottlenecks in your application? This can
be determined with profiling.





More information about the Twisted-Python mailing list