## Issue

I would like to generate multiple Erdos-Renyi graphs with random edge weights. However, my code works quite slow since there are two nested loops. I was wondering if someone can help me with improving my code.

```
import networkx as nx
import random
#Suppose I generate 1000 different random graphs
for _ in range(1000):
#Let's say I will have 100 nodes and the connection probability is 0.4
G= nx.fast_gnp_random_graph(100,0.4)
#Then, I assign random edge weights.
for (u, v) in G.edges():
G.edges[u,v]['weight'] = random.randint(15,5000)
```

When I run a similar code block in `R`

using `igraph`

, it is super fast regardless of the size of the network. What are some alternative ways that I can accomplish the same task without facing slow execution time?

## Solution

This benchmark shows the performance of many graphs libraries (from different languages). It confirms NetworkX is very slow. The graph-tool Python package seems a significantly faster alternative to NetworkX. Please note that the performance of a given package is dependent of what you want to achieve because the performance of a graph algorithm is very dependent of the chosen internal representation.

Answered By – Jérôme Richard

Answer Checked By – Cary Denson (BugsFixing Admin)