Printing out traces to be read by a memory simulation application in Python


Trying to print out large traces of memory addresses (32 bits) for 2^32 addresses in the following manner:

0x12345678 W

0x23456789 R






Basically going from one address to the other. So it could be going from 0x0000000 to 0x00000003, we would have 0x0000000 R, 0x0000001 R, 0x0000002 R, 0x0000003 W.

Main goal is to feed them to a simulator but I am having storage and memory issues printing these (using print()) and storing them in a file in Python. I can’t compress them because the simulator takes them in the above fashion. What is a good, efficient way to print them and store them in Python?

Asked By: winterlyrock



To efficiently generate and store 2^32 memory addresses in Python, consider writing them directly to a file in a loop without storing them in memory. Use string formatting for hexadecimal conversion and write each line to the file immediately. This approach minimizes memory usage.

basic script:

def generate_addresses(file_path):
    with open(file_path, 'w') as file:
        for i in range(2**32):
            address = f"0x{i:08X}"
            access_type = "W" if i % 2 == 0 else "R"
            file.write(f"{address} {access_type}n")

if __name__ == "__main__":

This script alternates between ‘W’ and ‘R’ for each address and writes directly to a file, which is efficient for such large-scale data handling.

Answered By: Saleh7

I would start by using just a file (for larger results) as print() is kind of slow. Maybe:

start = 0
end = 4
w_or_r = ["R", "W"]
with open("results.txt", "w") as file_out:
    for i in range(start, end):
        file_out.write(f"0x{i:08x} {w_or_r[i % 2]}n")

That should give you a file like:

0x00000000 R
0x00000001 W
0x00000002 R
0x00000003 W
Answered By: JonSG