Python value comparing taking too long Is there any way to make this code run faster. I have to compare values in a text file which is written in the format:

1 1.12345 

2 0.12345

3 3.45678...

The values are in billions and the code takes weeks to run. As far as I can see, the calculation (subtraction in my case) is the heavy step.

a=open("output.txt","w")

for i in range(1,num_lines+1): #num_lines are the number of lines in the file
    read=linecache.getline('file.txt',i).rstrip('\n') #reads line by line from a text file
    splitted=read.split()   #splits the string "read"

    for k in range(i+1,num_lines+1):        
        read1=linecache.getline('just_nodes.txt',k).rstrip('\n')
        splitted1=read1.split()

        alpha_temp=abs(float(splitted[1])-float(splitted1[1])) #heavy step

        alpha=round(alpha_temp,3)

        if (alpha==1.000):
            a.write(str(splitted1[0]) +' '+ str(splitted[0]) + "\n")
a.close()
One observation is that you are converting the splitted[1] to float inside the inner loop - redoing the same exact thing multiple times - do it once before the loop:

splitted = float(read.split(" ", 1)[1])  # convert to float here
for k in range(i + 1, num_lines + 1):        
    read1 = linecache.getline('just_nodes.txt', k).rstrip('\n')
    splitted1 = float(read1.split(" ", 1)[1])

    alpha_temp = abs(splitted - splitted1)


Note the other thing I've done - splitting by an explicit space and doing it strictly once - please check if it actually has a positive impact on performance.

You may also not strip the newlines from the end of the lines, float() can handle it:

In [1]: float("1.00\n")
Out[1]: 1.0