builtin function, sum, is much faster than for loop. The result on my machine is as follows
> def main(): > from timeit import timeit > data = bytearray().zfill(1500) > print 'new=', timeit(lambda : checksum(data), number=1000) > print 'old=', timeit(lambda : checksum_old(data), number=1000) > > new= 0.00800108909607 > old= 0.266770124435 Signed-off-by: Isaku Yamahata <[email protected]> --- ryu/lib/packet/packet_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ryu/lib/packet/packet_utils.py b/ryu/lib/packet/packet_utils.py index b8f2e05..c36be49 100644 --- a/ryu/lib/packet/packet_utils.py +++ b/ryu/lib/packet/packet_utils.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import array import socket import struct @@ -26,10 +27,10 @@ def checksum(data): if len(data) % 2: data += '\x00' - s = 0 - for i in range(0, len(data), 2): - w = data[i] + (data[i + 1] << 8) - s = carry_around_add(s, w) + data = str(data) # input can be bytearray. + s = sum(array.array('H', data)) + s = (s & 0xffff) + (s >> 16) + s += (s >> 16) return socket.ntohs(~s & 0xffff) -- 1.7.10.4 ------------------------------------------------------------------------------ Own the Future-Intel® Level Up Game Demo Contest 2013 Rise to greatness in Intel's independent game demo contest. Compete for recognition, cash, and the chance to get your game on Steam. $5K grand prize plus 10 genre and skill prizes. Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
