[Python] I trying to generate Erdös-Rényi Random Graphs faster with python using threading,...

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    This is the code I'm using, it take around 15-16 seconds to print the graph. I was also trying to make the threads uniquely giving for every a name and starting and join using that name, but no matter how many threads I was using the time wasn't changing much. I suspect that this is because of the way my generating function works, but I didn't really can know if this is true, maybe I am just using module in the wrong way

    import random
    import threading
    import math
    import time
    n=1000
    p=0.5
    G={}
    B=5
    def generator(a):
    for i in range((int((a-1)/B)*n),int((a/B))*n):
    for j in range(i+1, n):
    if random.random()>p:
    G.append(j)
    G[j].append(i)
    for k in range(n):
    G[k]=[]
    threads = []
    start_time=time.time()


    for a in range(1, B+1):
    thread = threading.Thread(target = generator, args=(a, ))
    thread.start()
    threads.append(thread)


    for thread in threads:
    thread.join()
    print(G)
    end_time1=time.time()
    print("Time taken1: {}".format(end_time1-start_time))

    Continue reading...

Compartilhe esta Página