Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function is slower in separate file

I have a following issue, there is a function to read a big file(few Mb), the content looks like:

0x05 0x00 0x00 0x00 0xCF 0x00 0x00 0x00 ; ..........
0xCF 0x00 0x00 0x00 0x22 0x00 0x00 0x00 ; ......"...
0x51 0x84 0x07 0x00 0x02 0x00 0x01 0x00 ; ..Q.......

My function has to read only the hex values, and to ignore ";" with following characters till the end of line.

Data from the first line what I need is

"0x05 0x00 0x00 0x00 0xCF 0x00 0x00 0x00 "

I tried two methods, one is with a separate file with this function

def ReadFileAsList(fileName):

    fileName = "2Output.txt"
    fileContentStr = ""
    with open(fileName,'r') as f:
        for line in f:
            fileContentStr += line.split(';')[0]

    fileContentList = fileContentStr.split()
    return fileContentList

and the second method, when these line are directly in my main .py file

fileName = "2Output.txt"
fileContentStr = ""
with open(fileName,'r') as f:
    for line in f:
        fileContentStr += line.split(';')[0]

fileContentList = fileContentStr.split()

The second method is very fast, the first(with the separate function in a separate file) is very slow, what am I missing? Thanks for any hint

like image 825
Andrey Mazur Avatar asked Apr 09 '26 18:04

Andrey Mazur


1 Answers

It is faster to store local variables than it is for global variables. Local variables are stored in a fixed-sized array, where as global variables are stored in a true dictionary.

Here is a link to a more in-depth answer

like image 167
badger0053 Avatar answered Apr 12 '26 09:04

badger0053



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!