try catch or type conversion performance in julia - (Julia 73 seconds, Python 0.5 seconds)
julia, performance, try-catch, type-conversion
Solution
Note also that you can use the float64_isvalid function in the standard library to (a) check whether a string is a valid floating-point value and (b) return the value.
Note also that the colons (`:`) after `try` and `catch` in your `isFloat` code are wrong in Julia (this is a Pythonism).
A much faster version of your code should be:
const isFloat2_out = [1.0]
isFloat2(s::String) = float64_isvalid(s, isFloat2_out)
function foo(L)
x = split(L, ",")
(all(isFloat2, x), x)
end
u = map(foo, open(readlines, "SMW100.asc"))
On my machine, for a sample file with 100,000 rows and 10 columns of data, 50% of which are valid numbers, your Python code takes 4.21 seconds and my Julia code takes 2.45 seconds.
Problem
I have been playing with Julia because it seems syntactically similar to python (which I like) but claims to be faster. However, I tried making a similar script to something I have in python for tesing where numerical values are within a text file which uses this function: ``` function isFloat(s) try: float64(s) return true catch: return false end end ``` For some reason, this takes a great deal of time for a text file with a reasonable amount of rows of text (~500000). Why would this be? Is there a better way to do this? What general feature of the language can I understand from this to apply to other languages? Here are the two exact scripts i ran with the times for reference: python: ~0.5 seconds ``` def is_number(s): try: np.float64(s) return True except ValueError: return False start = time.time() file_data = open('SMW100.asc').readlines() file_data = map(lambda line: line.rstrip('\n').replace(',',' ').split(), file_data) bools = [(all(map(is_number, x)), x) for x in file_data] print time.time() - start ``` julia: ~73.5 seconds ``` start = time() function isFloat(s) try: float64(s) return true catch: return false end end x = map(x-> split(replace(x, ",", " ")), open(readlines, "SMW100.asc")) u = [(all(map(isFloat, i)), i) for i in x] print(start - time()) ```