Skip to content

Instantly share code, notes, and snippets.

@wires
Created December 8, 2015 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wires/5918834712a64297d7d1 to your computer and use it in GitHub Desktop.
Save wires/5918834712a64297d7d1 to your computer and use it in GitHub Desktop.
Testing storage/loading of networkx graphs
from timeit import timeit
import pickle
from networkx.generators import fast_gnp_random_graph
from networkx.readwrite import json_graph
def create(N=1000):
# create some random graph
global G
G = fast_gnp_random_graph(N, 0.7)
def to_adjacency(G):
global data
data = json_graph.adjacency_data(G)
def dump(data):
with open('/tmp/graph.pickle', 'wb+') as f:
pickle.dump(data, f)
def load():
global data
with open('/tmp/graph.pickle', 'rb') as f:
data = pickle.load(f)
def from_adjacency(d):
global H
H = json_graph.adjacency_graph(d)
def time(what, code):
s = timeit(code, number=1, globals=globals())
print("\t{:04.3f}s ~ {}".format(s, what))
def timings(N):
print("\nN={}\n".format(N))
time("generating".format(N), "create({})".format(N))
time("converting", "to_adjacency(G)")
time("storing", "dump(data)")
time("loading", "load()")
time("converting", "from_adjacency(data)")
if __name__ == '__main__':
timings(1000)
timings(2000)
timings(3000)
timings(4000)
N=1000
0.666s ~ generating
0.790s ~ converting
0.237s ~ storing
0.295s ~ loading
1.152s ~ converting
N=2000
2.761s ~ generating
3.282s ~ converting
1.068s ~ storing
1.105s ~ loading
4.941s ~ converting
N=3000
6.377s ~ generating
7.644s ~ converting
2.464s ~ storing
2.393s ~ loading
12.219s ~ converting
N=4000
12.458s ~ generating
19.025s ~ converting
8.825s ~ storing
8.921s ~ loading
27.601s ~ converting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment