title: Optimize read from .gz file and cpu utilization python
body: <p>Sample Input File:</p>
  
  <pre><code>83,REGISTER,0,10.166.224.34,1518814163,[sip:1202677@mobile.com],sip:1202977@mobile.com,3727925550,0600,NULL,NULL                                                                                             
  83,INVITE,0,10.166.224.34,1518814163,[sip:1202687@mobile.com],sip:1202977@mobile.com,3727925550,0600,NULL,NULL
  83,INVITE,0,10.166.224.34,1518814163,[sip:1202677@mobile.com],sip:1202977@mobile.com,3727925550,0600,NULL,NULL
  83,REGISTER,0,10.166.224.34,1518814163,[sip:1202678@mobile.com],sip:1202977@mobile.com,3727925550,0600,NULL,NULL
  83,REGISTER,0,10.166.224.34,1518814163,[sip:1202687@mobile.com],sip:1202977@mobile.com,3727925550,0600,NULL,NULL
  </code></pre>
  
  <p>Sample Output File:</p>
  
  <pre><code>1202677 REGISTER,INVITE
  1202687 INVITE,REGISTER
  1202678 REGISTER
  </code></pre>
  
  <p>Code Sample:</p>
  
  <pre><code>filesList=glob.glob("%s/*.gz" %(sys.argv[1]))
  
  for file in filesList:
      try:
          fp = gzip.open(file, 'rb')
          f=fp.readlines()
          fp.close()
          for line in f:
              line = line.split(',')
              if line[0] == '83':
                  str=line[5].split("[sip:")
                  if len(str) &gt; 1:
                      str=str[1].split("@")
                  if dict.has_key(str[0].strip()):
                      dict[str[0].strip()] = dict.get(str[0].strip())+','+line[1]
                  else:
                      dict[str[0].strip()] = line[1]
      except:
          print "Unexpected Error: ", sys.exc_info()[0]
  
  try:
      with open(sys.argv[2],'w') as s:
          for num in dict:
              print &gt;&gt; s, num,dict[num]
  except:
      print "Unexpected error:", sys.exc_info()[0]
  </code></pre>
  
  <p>When I run above script with <em>2.1GB(430 files)</em> load then It took approx 13 minute to execute and CPU utilization was approx 100%.</p>
  
  <pre><code>PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                              
  12586 root      20   0  156m 134m 1808 R 99.8  0.2   0:40.17 script
  </code></pre>
  
  <p>Please let me know that how I can optimize above code to reduce execution time. Thanks</p>