Find nodes with caching

find2.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# if you have many nodes and call findNode quite often you
 4# can use initFindCache() to speed it up. On a scene with
 5# 10000 nodes it is about 700 times faster!
 6newScene()
 7
 8import time
 9
10print("Executing find2 script!")
11
12# number of nodes to create and search
13n = 5000
14
15print("Started creating nodes")
16name = "name"
17for i in range(n):
18    group = createNode("Group", name + str(i))
19    
20print("\nFinding nodes without cache\n")
21# now find via a list of names
22names = []
23for i in range(n):
24    names.append(name + str(i))
25
26t = time.time()
27for i in range(n):
28    node = findNode(name + str(i))
29print("Time needed for finding node separately: ", time.time() - t)
30
31t = time.time()
32nodes = findNodes(names)
33print("Time needed for finding node list: ", time.time() - t)
34
35print("\nNow using the cache:\n")
36
37t = time.time()
38# cache all nodes, gives a great speed improvement!
39initFindCache()
40print("Time needed to create cache: ", time.time() - t)
41
42t = time.time()
43for i in range(n):
44    node = findNode(name + str(i))
45print("Time needed for finding node separately: ", time.time() - t)
46
47t = time.time()
48nodes = findNodes(names)
49print("Time needed for finding node list: ", time.time() - t)
50clearFindCache()