Is
text.startswith(\'a\')
better than
text[0]==\'a\'
?
Knowing text is not empty and we a
def compo2():
n = "abba"
for i in range(1000000):
n[:1]=="_"
is faster than
def compo():
n = "abba"
for i in range(1000000):
n.startswith("_")
cProfile reports 0.061 for compo2
compared to 0.954 for compo
on my machine.
This of interest in case you want to make a LOT of prefix checks for various "_mystring".
If most strings don't start with underscores then using string[:1]== char
before using startswith
is an option to optimize your code.
In a real application this method saved me about 15% of cpu time.
Yes: it’s easier to use and easier to read. When you are testing for more than one letter, when using slicing, you’ll have to know how long the target text is:
haystack = 'Hello, World!'
needle = 'Hello'
# The easy way
result = haystack.startswith(needle)
# The slightly harder way
result = haystack[:len(needle)] == needle
Edit: The question seems to have changed. It now says, “knowing text is not empty and we are only interested in the first character of it.” That turns it into a fairly meaningless hypothetical situation.
I suspect the questioner is trying to “optimize” his/her code for execution speed. If that is the case, my answer is: don’t. Use whichever form is more readable and, therefore, more maintainable when you have to come back and work on it a year from now. Only optimize if profiling shows that line of code to be the bottleneck. This is not some O(n²) algorithm. It’s a string comparison.
The stock phrase for the questiom is: "Premature optimization is the root of all evil".
text[0]
fails if text
is an empty string:
IronPython 2.6 Alpha (2.6.0.1) on .NET 4.0.20506.1
Type "help", "copyright", "credits" or "license" for more information.
>>> text = ""
>>> print(text.startswith("a"))
False
>>> print(text[0]=='a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index out of range: 0
EDIT: You say you "know" that text
is not empty... how confident are you of that, and what would you want to happen if it is empty in reality? If a failure is appropriate (e.g. it means a bug in your code) that would encourage the use of text[0]=='a'
.
Other questions:
How concerned are you about the performance of this? If this is performance critical, then benchmark it on your particular Python runtime. I wouldn't be entirely surprised to find that (say) one form was faster on IronPython and a different one faster on CPython.
Which do you (and your team) find more readable?
text[0]=='a' is good considering performance. Also you need to add validation when you use this.
My ideas:
startswith
can work with any length, can work with empty strings, I prefer this.
indexing can only work if the length is specified, cannot work with empty strings.