现在的位置: 首页 > 综合 > 正文

(pbc lua 加入)c++_lua_Python with/without extension性能测试 (10万次SerializeToString & ParseFromString)

2012年03月11日 ⁄ 综合 ⁄ 共 3300字 ⁄ 字号 评论关闭

A. extension

   c++: 0.259s

   lua:6.136s

   python: 15.874s

    c++比 lua  -->23.6倍

    lua比python -->2.58倍

B.without extension

    c++:0.098s

    林卓毅 protoc-gen-lua lua:1.788s

    云风 pbc lua: message mode 0.585s, pack mode0.187s

    python: 4.645s

    c++比 lua  -->18.2倍

    lua比python -->2.59倍

c++ 带extension , 慢2.64

lua  带extension, 慢3.43

python 带extension, 慢3.41

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

测试环境:vmware Debian6 , time函数

gcc 4.4.5

lua 5.1.4

python 2.6.6

protobuf 2.4.1

------------------c++------------------------------

#include "person.pb.h"
#include <iostream>
#include <stdio.h>
//#include <windows.h>
int main(int argc, char* argv[])
{
	GOOGLE_PROTOBUF_VERIFY_VERSION;
	//printf("%s\n","hello");
	Person person;
	person.set_id(1000);
	person.set_name("Alice");
	person.set_email("Alice@example.com");
	/*
	Phone* pPhone1 = person.AddExtension(Phone::phones);
	pPhone1->set_num("2147483647");
	pPhone1->set_type(Phone_PHONE_TYPE_HOME);
	Phone* pPhone2 = person.AddExtension(Phone::phones);
	pPhone2->set_num("123456789");
	pPhone2->set_type(Phone_PHONE_TYPE_MOBILE);
	
	//int aaa = person.ExtensionSize(Phone::phones);
	//Phone pPhone3 = person.GetExtension(Phone::phones, 1);
	*/
	std::string data;
	Person personShow;
	//DWORD dwStart = GetTickCount();
	for(int i=0; i< 100000; i++)
	{
		person.SerializeToString(&data);
		personShow.ParseFromString(data);
	}
	//DWORD dwUse = GetTickCount() - dwStart;
	//printf("%d\n",dwUse);
	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}

-------------------------------------------lua------------------------------------------

package.path = package.path .. ';../protobuf/?.lua'
package.cpath = package.cpath .. ';../protobuf/?.so'

require 'person_pb'
local person= person_pb.Person()
person.id = 1000
person.name = "Alice"
person.email = "Alice@example.com"
--[[
local home = person.Extensions[person_pb.Phone.phones]:add()
home.num = "2147483647"
home.type = person_pb.Phone.HOME

local home2 = person.Extensions[person_pb.Phone.phones]:add()
home2.num = "2147483647"
home2.type = person_pb.Phone.MOBILE
]]
local msg = person_pb.Person()
local data
for i=1, 100000 do
	data = person:SerializeToString()
	msg:ParseFromString(data)
end
--print(msg)

----------------------------------------------PBC lua--------------------------------------

local protobuf = require "protobuf"

addr = io.open("../../build/addressbook.pb","rb")
buffer = addr:read "*a"
addr:close()
protobuf.register(buffer)

local person = {
	name = "Alice",
	email = "Alice@example.com",
	id = 1000,
--	phone = {
--		{ number = "123456789" , type = "MOBILE" },
--		{ number = "87654321" , type = "HOME" },
--	}
}

local buffer
local t
for i=1, 100000 do
	--buffer = protobuf.encode("tutorial.Person", person)
	-- t = protobuf.decode("tutorial.Person", buffer)
	buffer = protobuf.pack("tutorial.Person name id email","Alice",1000,"Alice@example.com")
	protobuf.unpack("tutorial.Person name id email", buffer )
end
--[[
for k,v in pairs(t) do
	if type(k) == "string" then
		print(k,v)
	end
end

print(t.phone[2].type)

for k,v in pairs(t.phone[1]) do
	print(k,v)
end
]]

---------------------------------------------python------------------------------------------

#! /usr/bin/python
import person_pb2

person = person_pb2.Person()
person.id = 1000
person.name = "Alice"
person.email = "Alice@example.com"
personMsg = person_pb2.Person()
"""
phone1 = person.Extensions[person_pb2.Phone.phones].add()
phone1.num = "2147483647"
phone1.type = person_pb2.Phone.HOME
phone2 = person.Extensions[person_pb2.Phone.phones].add()
phone2.num = "2147483647"
phone2.type = person_pb2.Phone.MOBILE
"""
for i in range(100000):
    data = person.SerializeToString()
    personMsg .ParseFromString(data)

抱歉!评论已关闭.