I use protobuf in python. if I new a unused variable like "sssssss = [{'a': 
1}, {'b': 2}] * 10000000", protobuf function like "add" or "extend" wile 
cost much more time sometimes, And if I del variable sssssss,  it will be 
normal.


pb:

syntax = "proto3";
option go_package = "pb/pushmessage";
package hslevel2quote;

message FieldMapEntry {
    string Key = 1;
    string Value = 2;
}
message CodeMapEntry {
    string Code = 1;
    repeated FieldMapEntry FieldMap = 2;
}

message StreamResponse {
    string ServerId = 1; 
    string SendTime = 2; 
    repeated CodeMapEntry CodeItem = 3;
    repeated FieldMapEntry FieldItem = 4;
}
message StreamRequest {
    int32 Type = 1; 
    int32 Version = 2; 
}


code:

FIELD_SEQ_MAP = {
    'order_bid': '52',
    'order_ask': '53',
    'broker_buy': '54',
    'broker_sell': '55',
    'mingxi': '56',
}

FIELD_SEQ_VALUE = FIELD_SEQ_MAP.values()


def make_stream_response(all_data):
    response = pushmessage_pb2.StreamResponse()
    response.ServerId = "hkLevel2PushServer"
    response.SendTime = time.strftime("%Y-%m-%d %H:%M:%S")

    for stock_code, data_map in all_data.items():
        stock_code = 'hk' + stock_code
        code_entry = response.CodeItem.add()
        code_entry.Code = stock_code
        for key, data in data_map.items():
            try:
                if FIELD_SEQ_MAP.has_key(key):
                    # value = cjson.encode(data)
                    if key in ['order_bid', 'order_ask']:
                        value = ','.join(['/'.join(['%s' % v for v in d]) for d 
in data])
                    else:
                        value = ','.join(data)
                    field_entry = code_entry.FieldMap.add()
                    field_entry.Key = FIELD_SEQ_MAP[key]
                    field_entry.Value = value
                elif key == 'qt':
                    for seq, value in data.items():
                        if seq not in FIELD_SEQ_VALUE:
                            field_entry = code_entry.FieldMap.add()
                            field_entry.Key = seq
                            field_entry.Value = value
            except Exception as e:
                log.msg('HK_LEVEL2', 'ERROR',
                        'make_code_entry error key[%s], value[%s], error[%s]' % 
(key, data, vosExcept.exceptMsg()))
                continue

    return response


def test():
    import cjson

    sssssss = [{'a': 1}, {'b': 2}] * 10000000
    with open('alldata.txt', 'r') as f:
        content = f.read()
    line_list = content.split("\n")
    for i in xrange(1, 100):
        for line in line_list:
            try:
                time1 = int(time.time() * 1000)
                info = cjson.decode(line)
                make_stream_response(info)
                time2 = int(time.time() * 1000)
                print len(info), time2 - time1
            except Exception as e:
                continue

if __name__ == '__main__':
    test()


env: python2.7, protobuf3.5.1



-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to