#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    String find increase speed


    Since I got my Raspberry Pi, I wanted to use it and since it is best to use Python I learned Python. I came from Java and web development, especially PHP. So I started writing a simple object oriented program so I would get stuff easily later on. But since I have to think about speed on the RPi, I need to make the code as efficient as possible.

    The current class that I'm working on is reading my latest mail and things related to my mail. Everything goes fine, but I'm working with quite large strings to search something in using the "find()" method on strings. I'm getting everything just fine as I would want it. As I said I need to focus on efficiency, so I need to make it faster.

    The code is the following:
    python Code:
    def getInbox(self, need):
    		try:
    			msg_amount = len(self.POP3.list()[1])
    			find = [['From', '<', '>'], 'Subject']
    			for msg_number in xrange(need):
    				msg_content = str(self.POP3.retr(msg_amount-msg_number)[1])
     
    				# Go through the objects in the list "find"
    				for f in find:
    					# If "f" is a list, then use the first object of that list as the search string
    					islist = (type(f) == list)
    					if(islist):
    						f_search = f[0]
    					# If not then use f as the search string	
    					else:
    						f_search = f
    					f_search += ': '
    					f_start = msg_content.find(f_search)
    					# f_start will be -1 if nothing has been found
    					if f_start == -1:
    						print f_search + ' has not been found in the message.'
    					else:
    						# If "f" is a list then we want a more specific result (e.g. the email address)
    						if(islist):
    							f_start = msg_content.find(f[1], f_start)
    						f_end = msg_content.find('\',', f_start)
    						if(islist):
    							f_end = msg_content.find(f[2], f_start)
    						f_word = msg_content[f_start+len(f_search):f_end]
    						print 'f = ' + f_search + ', found word: ' + f_word
    				# Debug purposes
    				print '\n --- \n'
    		except (poplib.error_proto), e:
    			print 'Could not get list from the POP3 server.'
    			print str(e)


    The POP3 connection is set in the constructor if you wondered where it was. You see that I need to find a string in a quite big string of text (msg_content) and I don't know how the "find()" method works and wondered whether I can make it better. If you have any idea or comment, please tell me because I'm already glad I signed up here.
    Last edited by Dreeass; April 10th, 2013 at 04:16 PM. Reason: Edited code tag to highlight Python
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    You won't beat str.find by writing additional python code. I don't know what search technique your python implementation uses---probably Boyer–Moore.

    If your code is slow use a (link) profiler to determine where it is slow. Concentrate your effort there.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You won't beat str.find by writing additional python code. I don't know what search technique your python implementation uses---probably Boyer–Moore.

    If your code is slow use a (link) profiler to determine where it is slow. Concentrate your effort there.
    Well that brings me to another point, a Python IDE. Since I do most of my beginning Python work on a Raspberry Pi and not on a standard computer I'd like to know some good Python IDE's. But as you probably know that the RPi isn't a speedmonster and I need a good lightweight IDE but I'd like to have the following features:
    1. Configurable syntax hightlighting
    2. Lightweight as I said, Geany lags when I type
    3. Built-in terminal to run my code in
    4. Debugger but this is optional
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I avoid "ide"s as much as possible, which isn't completely.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I avoid "ide"s as much as possible, which isn't completely.
    Well I used to use VIM for a while but it just isn't for me. I have added so much things to it, but still the terminal feel of it doesn't appeal to me.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I suppose I should try vim. vi was and is ridiculous, I don't imagine a gui version of vi would be useful.

    Use emacs.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I suppose I should try vim. vi was and is ridiculous, I don't imagine a gui version of vi would be useful.

    Use emacs.
    Well before I was using VIM I tried eMacs but I couldn't get it to work for some reason and gave up on it. But don't you have any Python IDE's for the RPi?
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    I would think that idle runs on the Rasberry pi. You want an IDE and I'm the wrong person to recommend one.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I would think that idle runs on the Rasberry pi. You want an IDE and I'm the wrong person to recommend one.
    Ok, thanks for helping me though.

IMN logo majestic logo threadwatch logo seochat tools logo