Pella86

Python Performance and constructors

Jul 27th, 2017
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # Python Performance and constructors
  2.  
  3. '''This is the type of data I get, is a dictionary (I think is derived from a JSON object)'''
  4. query_message = {'id': '000000000000000000',
  5.  'from': {'id': 000000000, 'first_name': 'AAAAAAAAAAAAA', 'username': 'AAAAAAAAAA', 'language_code': 'aa-AA'},
  6.  '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'},
  7.  'chat_instance': 'AAAAAAAAAAAAAAAAAAAA',
  8.  'data': 'AAAAAAAAA'}
  9.  
  10. ''' I constructed my class to parse it this way:
  11. since I will get many of this objects, and I don't want to process them all
  12. I thought to skip some steps
  13. I would like tho to keep the option to look at the arguments later on
  14. now with glance the class is built without the optional arguments
  15. '''
  16.  
  17. class CbkQuery:
  18.    
  19.     def __init__(self, query_msg, glance = False, opt = True):
  20.        
  21.         #required arguments
  22.         self.id = query_msg['id']
  23.         self.person = Person(query_msg['from'])
  24.         self.chat_instance = query_msg['chat_instance']
  25.        
  26.         if glance:
  27.             pass
  28.         else:
  29.             self.query_msg = query_msg
  30.             if opt:
  31.                 # optional arguments
  32.                 self.data = self.query_msg.get('data')
  33.                 self.message = self.query_msg.get('message')
  34.                 if self.message is not None:
  35.                     self.message = Message(self.message)
  36.                 self.inline_message_id = self.query_msg.get('inline_message_id')
  37.                 self.game_short_name = self.query_msg.get('game_short_name')
  38.  
  39. ''' Since I did this gimmick in order to have a fast reading, I wanted to time it
  40. and see how much I save by "glancing" the class
  41. '''
  42.  
  43. # test 1M times each constructor possibility
  44. print("\n MY MESSAGE ")
  45.  
  46. lg.startTimer()
  47. for i in range(1000000):
  48.     qmsg = CbkQuery(msg, glance=True, opt=False)
  49. lg.log("glance processing took: ", True)
  50. # glance processing took:
  51. # Elapsed time for subprocess: 00:00:01.749
  52.  
  53. lg.startTimer()
  54. for i in range(100000):
  55.     qmsg = CbkQuery(msg, glance=False, opt=False)
  56. lg.log("store query, processing took: ", True)
  57. # store query, processing took:
  58. # Elapsed time for subprocess: 00:00:00.182
  59.    
  60. lg.startTimer()
  61. for i in range(100000):
  62.     qmsg = CbkQuery(msg)
  63. lg.log("build complete class processing took: ", True)
  64. # build complete class processing took:
  65. # Elapsed time for subprocess: 00:00:01.322
  66.  
  67. ''' surprise surprise, glancing the class takes longer, the fastest is to store the "unprocessed message in the class"
  68. can somebody explain me why? is it because storing the unprocessed message actually makes a shallow copy instead of a copy?
  69. '''
Advertisement
Add Comment
Please, Sign In to add comment