Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Performance and constructors
- '''This is the type of data I get, is a dictionary (I think is derived from a JSON object)'''
- query_message = {'id': '000000000000000000',
- 'from': {'id': 000000000, 'first_name': 'AAAAAAAAAAAAA', 'username': 'AAAAAAAAAA', 'language_code': 'aa-AA'},
- 'message': {'message_id': 0000, 'from': {'id': 000000000,'first_name': 'AAAAAAAAAAAAAA', 'username': 'AAAAAAAAAAAA'},'chat': {'id': 000000000, 'first_name': 'AAAAAAAAAAAAA', 'username': 'AAAAAAAAAA', 'type': 'private'}, 'date': 1501131446, 'document': {'file_name': 'AAAAAAA.mp4', 'mime_type': 'video/mp4', 'thumb': {'file_id': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'file_size': 3262, 'file_path': 'thumbnails/20859.jpg', 'width': 90, 'height': 67}, 'file_id': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'file_size': 766649}, 'caption': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'},
- 'chat_instance': 'AAAAAAAAAAAAAAAAAAAA',
- 'data': 'AAAAAAAAA'}
- ''' I constructed my class to parse it this way:
- since I will get many of this objects, and I don't want to process them all
- I thought to skip some steps
- I would like tho to keep the option to look at the arguments later on
- now with glance the class is built without the optional arguments
- '''
- class CbkQuery:
- def __init__(self, query_msg, glance = False, opt = True):
- #required arguments
- self.id = query_msg['id']
- self.person = Person(query_msg['from'])
- self.chat_instance = query_msg['chat_instance']
- if glance:
- pass
- else:
- self.query_msg = query_msg
- if opt:
- # optional arguments
- self.data = self.query_msg.get('data')
- self.message = self.query_msg.get('message')
- if self.message is not None:
- self.message = Message(self.message)
- self.inline_message_id = self.query_msg.get('inline_message_id')
- self.game_short_name = self.query_msg.get('game_short_name')
- ''' Since I did this gimmick in order to have a fast reading, I wanted to time it
- and see how much I save by "glancing" the class
- '''
- # test 1M times each constructor possibility
- print("\n MY MESSAGE ")
- lg.startTimer()
- for i in range(1000000):
- qmsg = CbkQuery(msg, glance=True, opt=False)
- lg.log("glance processing took: ", True)
- # glance processing took:
- # Elapsed time for subprocess: 00:00:01.749
- lg.startTimer()
- for i in range(100000):
- qmsg = CbkQuery(msg, glance=False, opt=False)
- lg.log("store query, processing took: ", True)
- # store query, processing took:
- # Elapsed time for subprocess: 00:00:00.182
- lg.startTimer()
- for i in range(100000):
- qmsg = CbkQuery(msg)
- lg.log("build complete class processing took: ", True)
- # build complete class processing took:
- # Elapsed time for subprocess: 00:00:01.322
- ''' surprise surprise, glancing the class takes longer, the fastest is to store the "unprocessed message in the class"
- can somebody explain me why? is it because storing the unprocessed message actually makes a shallow copy instead of a copy?
- '''
Advertisement
Add Comment
Please, Sign In to add comment