I played with Julia because she seems syntactically similar to python (I like), but claims to be faster. However, I tried to do a similar script to what I have in python for tesing, where the numerical values ββare in a text file that uses this function:
function isFloat(s)
try:
float64(s)
return true
catch:
return false
end
end
For some reason, it takes a long time for a text file with a reasonable number of lines of text (~ 500000).
Why should it be? Is there a better way to do this? What common language feature can I understand from this to apply to other languages?
Here are two exact scripts that I ran over time 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())