You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Or not. Both C++ and Python will successfully convert scientific notation such as -7.0752597e+1, or values of +- INF or NAN.
C++ will parse hex values such as -0x66.C; Python will not.
It makes no sense to allow these esoteric strings that would not be produced by wptedit. A stray e or E could find its way into a latitude or longitude string just as easily that a did in rou.dj102k.wpt that one time. A NAN or INF could have unintended consequences farther down the line.
C++, we can clean up using an inline function. Python, a function may harm performance by introducing more passes by value.
In either case, efficiency can be improved over what we have now.
C++: We're down 49 lines in the Waypoint ctor itself, 29 lines overall. Any increase in speed I might see may just be confirmation bias, but it's not any slower. :) The code should be faster in theory, at any rate...
Not checking for a - at the beginning
Not checking whether we're past the beginning when checking for - thereafter.
A couple < and >s should be more efficient than using strchr.
Python: I didn't really expect it to help much, but for giggles I implemented pretty much a carbon copy of the C++ code:
defvalid_num_str(data):
point_count=0# check initial characterifdata[0] =='.':
point_count=1elifdata[0] <'0'anddata[0] !='-'ordata[0] >'9':
returnFalse# check subsequent charactersforcindata[1:]:
# check for minus sign not at beginningifc=='-':
returnFalse# check for multiple decimal pointsifc=='.':
point_count+=1ifpoint_count>1:
returnFalse# check for invalid characterselifc<'0'orc>'9':
returnFalsereturnTrue
It turned out to be a tiny bit faster, a bit surprising since I'd expect there to be more passing around strings by value, and more copying. I don't really know how Python works under the hood, or what optimizations the interpreter may have. Maybe the same changes made in the C++ version were enough to have a bigger effect here...
In that vein, does slicing the string (for c in data[1:]) create a new string object? Would it be faster to use a numeric index range, and iterate thru the chars in the existing string? Something to try out. (Slower than the original!)
Can we still improve speed by inlining this code, and avoiding the function call & string copying? If I can get a decent speedup Reading waypoints for all routes, I'll take that over brevity. (Nope. Maybe a bit slower; within margin of error. The tidy version is also the fastest.
Activity
yakra commentedon Apr 12, 2020
Or not. Both C++ and Python will successfully convert scientific notation such as
-7.0752597e+1, or values of +-INForNAN.C++ will parse hex values such as
-0x66.C; Python will not.It makes no sense to allow these esoteric strings that would not be produced by wptedit. A stray
eorEcould find its way into a latitude or longitude string just as easily thatadid in rou.dj102k.wpt that one time. ANANorINFcould have unintended consequences farther down the line.C++, we can clean up using an inline function.
Python, a function may harm performance by introducing more passes by value.
yakra commentedon Apr 13, 2020
C++: We're down 49 lines in the Waypoint ctor itself, 29 lines overall. Any increase in speed I might see may just be confirmation bias, but it's not any slower. :) The code should be faster in theory, at any rate...
-at the beginning-thereafter.<and>s should be more efficient than usingstrchr.Python: I didn't really expect it to help much, but for giggles I implemented pretty much a carbon copy of the C++ code:
It turned out to be a tiny bit faster, a bit surprising since I'd expect there to be more passing around strings by value, and more copying. I don't really know how Python works under the hood, or what optimizations the interpreter may have. Maybe the same changes made in the C++ version were enough to have a bigger effect here...
for c in data[1:]) create a new string object? Would it be faster to use a numeric index range, and iterate thru the chars in the existing string? Something to try out. (Slower than the original!)