How to speed up python instance initialization for millions of objects?











up vote
2
down vote

favorite












I have defined a python class named Edge as follows:



class Edge:
def __init__(self):
self.node1 = 0
self.node2 = 0
self.weight = 0


Now I have to create approximately 10^6 to 10^7 instances of Edge using:



edges= 
for (i,j,w) in ijw:
edge = Edge()
edge.node1 = i
edge.node2 = j
edge.weight = w
edges.append(edge)


I took me approximately 2 seconds in Desktop. Is there any faster way to do?










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have defined a python class named Edge as follows:



    class Edge:
    def __init__(self):
    self.node1 = 0
    self.node2 = 0
    self.weight = 0


    Now I have to create approximately 10^6 to 10^7 instances of Edge using:



    edges= 
    for (i,j,w) in ijw:
    edge = Edge()
    edge.node1 = i
    edge.node2 = j
    edge.weight = w
    edges.append(edge)


    I took me approximately 2 seconds in Desktop. Is there any faster way to do?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have defined a python class named Edge as follows:



      class Edge:
      def __init__(self):
      self.node1 = 0
      self.node2 = 0
      self.weight = 0


      Now I have to create approximately 10^6 to 10^7 instances of Edge using:



      edges= 
      for (i,j,w) in ijw:
      edge = Edge()
      edge.node1 = i
      edge.node2 = j
      edge.weight = w
      edges.append(edge)


      I took me approximately 2 seconds in Desktop. Is there any faster way to do?










      share|improve this question















      I have defined a python class named Edge as follows:



      class Edge:
      def __init__(self):
      self.node1 = 0
      self.node2 = 0
      self.weight = 0


      Now I have to create approximately 10^6 to 10^7 instances of Edge using:



      edges= 
      for (i,j,w) in ijw:
      edge = Edge()
      edge.node1 = i
      edge.node2 = j
      edge.weight = w
      edges.append(edge)


      I took me approximately 2 seconds in Desktop. Is there any faster way to do?







      python performance instance






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 22:16









      Martijn Pieters

      696k12924052245




      696k12924052245










      asked Nov 20 at 7:52









      ted930511

      17312




      17312
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          You can't make it much faster, but I certainly would use __slots__ to save on memory allocations. Also make it possible to pass in the attribute values when creating the instance:



          class Edge:
          __slots__ = ('node1', 'node2', 'weight')
          def __init__(self, node1=0, node2=0, weight=0):
          self.node1 = node1
          self.node2 = node2
          self.weight = weight


          With the updated __init__ you can use a list comprehension:



          edges = [Edge(*args) for args in ijw]


          Together these can shave off a decent amount of time creating the objects, roughly halve the time needed.



          Comparison creating 1 million objects; the setup:



          >>> from random import randrange
          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 6)]
          >>> class OrigEdge:
          ... def __init__(self):
          ... self.node1 = 0
          ... self.node2 = 0
          ... self.weight = 0
          ...
          >>> origloop = '''
          ... edges=
          ... for (i,j,w) in ijw:
          ... edge = Edge()
          ... edge.node1 = i
          ... edge.node2 = j
          ... edge.weight = w
          ... edges.append(edge)
          ... '''
          >>> class SlotsEdge:
          ... __slots__ = ('node1', 'node2', 'weight')
          ... def __init__(self, node1=0, node2=0, weight=0):
          ... self.node1 = node1
          ... self.node2 = node2
          ... self.weight = weight
          ...
          >>> listcomploop = '''[Edge(*args) for args in ijw]'''


          and the timings:



          >>> from timeit import Timer
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          722.1121070033405
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          386.6706900007557


          That's nearly 2 times as fast.



          Increasing the random input list to 10^7 items, and the timing difference holds:



          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 7)]
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count)
          7.183759553998243
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count)
          3.8709938440006226





          share|improve this answer























          • Thanks, I will try and test how much it improves!
            – ted930511
            Nov 20 at 8:05










          • Will dataclasses do anything for instantiation speed?
            – Tomalak
            Nov 20 at 8:08






          • 1




            @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
            – Martijn Pieters
            Nov 20 at 10:08






          • 1




            Thanks for clearing that up!
            – Tomalak
            Nov 20 at 10:17






          • 1




            @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
            – Martijn Pieters
            Nov 20 at 10:29




















          up vote
          1
          down vote













          Another option is to skip the Edge class and implement the edges via a table, or adjacency matrix.



          E.g.



          A = create_adjacency_graph(ijw)  # Implement to return a IxJ (sparse?) matrix of weights
          edge_a_weight = A[3, 56]
          edge_b_weight = A[670, 1023]
          # etc...


          This does remove some flexibility though, but should be quite fast both to create and use.






          share|improve this answer





















          • In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
            – ted930511
            Nov 20 at 8:03










          • @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
            – B. M.
            Nov 20 at 8:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388451%2fhow-to-speed-up-python-instance-initialization-for-millions-of-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote



          accepted










          You can't make it much faster, but I certainly would use __slots__ to save on memory allocations. Also make it possible to pass in the attribute values when creating the instance:



          class Edge:
          __slots__ = ('node1', 'node2', 'weight')
          def __init__(self, node1=0, node2=0, weight=0):
          self.node1 = node1
          self.node2 = node2
          self.weight = weight


          With the updated __init__ you can use a list comprehension:



          edges = [Edge(*args) for args in ijw]


          Together these can shave off a decent amount of time creating the objects, roughly halve the time needed.



          Comparison creating 1 million objects; the setup:



          >>> from random import randrange
          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 6)]
          >>> class OrigEdge:
          ... def __init__(self):
          ... self.node1 = 0
          ... self.node2 = 0
          ... self.weight = 0
          ...
          >>> origloop = '''
          ... edges=
          ... for (i,j,w) in ijw:
          ... edge = Edge()
          ... edge.node1 = i
          ... edge.node2 = j
          ... edge.weight = w
          ... edges.append(edge)
          ... '''
          >>> class SlotsEdge:
          ... __slots__ = ('node1', 'node2', 'weight')
          ... def __init__(self, node1=0, node2=0, weight=0):
          ... self.node1 = node1
          ... self.node2 = node2
          ... self.weight = weight
          ...
          >>> listcomploop = '''[Edge(*args) for args in ijw]'''


          and the timings:



          >>> from timeit import Timer
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          722.1121070033405
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          386.6706900007557


          That's nearly 2 times as fast.



          Increasing the random input list to 10^7 items, and the timing difference holds:



          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 7)]
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count)
          7.183759553998243
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count)
          3.8709938440006226





          share|improve this answer























          • Thanks, I will try and test how much it improves!
            – ted930511
            Nov 20 at 8:05










          • Will dataclasses do anything for instantiation speed?
            – Tomalak
            Nov 20 at 8:08






          • 1




            @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
            – Martijn Pieters
            Nov 20 at 10:08






          • 1




            Thanks for clearing that up!
            – Tomalak
            Nov 20 at 10:17






          • 1




            @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
            – Martijn Pieters
            Nov 20 at 10:29

















          up vote
          8
          down vote



          accepted










          You can't make it much faster, but I certainly would use __slots__ to save on memory allocations. Also make it possible to pass in the attribute values when creating the instance:



          class Edge:
          __slots__ = ('node1', 'node2', 'weight')
          def __init__(self, node1=0, node2=0, weight=0):
          self.node1 = node1
          self.node2 = node2
          self.weight = weight


          With the updated __init__ you can use a list comprehension:



          edges = [Edge(*args) for args in ijw]


          Together these can shave off a decent amount of time creating the objects, roughly halve the time needed.



          Comparison creating 1 million objects; the setup:



          >>> from random import randrange
          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 6)]
          >>> class OrigEdge:
          ... def __init__(self):
          ... self.node1 = 0
          ... self.node2 = 0
          ... self.weight = 0
          ...
          >>> origloop = '''
          ... edges=
          ... for (i,j,w) in ijw:
          ... edge = Edge()
          ... edge.node1 = i
          ... edge.node2 = j
          ... edge.weight = w
          ... edges.append(edge)
          ... '''
          >>> class SlotsEdge:
          ... __slots__ = ('node1', 'node2', 'weight')
          ... def __init__(self, node1=0, node2=0, weight=0):
          ... self.node1 = node1
          ... self.node2 = node2
          ... self.weight = weight
          ...
          >>> listcomploop = '''[Edge(*args) for args in ijw]'''


          and the timings:



          >>> from timeit import Timer
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          722.1121070033405
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          386.6706900007557


          That's nearly 2 times as fast.



          Increasing the random input list to 10^7 items, and the timing difference holds:



          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 7)]
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count)
          7.183759553998243
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count)
          3.8709938440006226





          share|improve this answer























          • Thanks, I will try and test how much it improves!
            – ted930511
            Nov 20 at 8:05










          • Will dataclasses do anything for instantiation speed?
            – Tomalak
            Nov 20 at 8:08






          • 1




            @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
            – Martijn Pieters
            Nov 20 at 10:08






          • 1




            Thanks for clearing that up!
            – Tomalak
            Nov 20 at 10:17






          • 1




            @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
            – Martijn Pieters
            Nov 20 at 10:29















          up vote
          8
          down vote



          accepted







          up vote
          8
          down vote



          accepted






          You can't make it much faster, but I certainly would use __slots__ to save on memory allocations. Also make it possible to pass in the attribute values when creating the instance:



          class Edge:
          __slots__ = ('node1', 'node2', 'weight')
          def __init__(self, node1=0, node2=0, weight=0):
          self.node1 = node1
          self.node2 = node2
          self.weight = weight


          With the updated __init__ you can use a list comprehension:



          edges = [Edge(*args) for args in ijw]


          Together these can shave off a decent amount of time creating the objects, roughly halve the time needed.



          Comparison creating 1 million objects; the setup:



          >>> from random import randrange
          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 6)]
          >>> class OrigEdge:
          ... def __init__(self):
          ... self.node1 = 0
          ... self.node2 = 0
          ... self.weight = 0
          ...
          >>> origloop = '''
          ... edges=
          ... for (i,j,w) in ijw:
          ... edge = Edge()
          ... edge.node1 = i
          ... edge.node2 = j
          ... edge.weight = w
          ... edges.append(edge)
          ... '''
          >>> class SlotsEdge:
          ... __slots__ = ('node1', 'node2', 'weight')
          ... def __init__(self, node1=0, node2=0, weight=0):
          ... self.node1 = node1
          ... self.node2 = node2
          ... self.weight = weight
          ...
          >>> listcomploop = '''[Edge(*args) for args in ijw]'''


          and the timings:



          >>> from timeit import Timer
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          722.1121070033405
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          386.6706900007557


          That's nearly 2 times as fast.



          Increasing the random input list to 10^7 items, and the timing difference holds:



          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 7)]
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count)
          7.183759553998243
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count)
          3.8709938440006226





          share|improve this answer














          You can't make it much faster, but I certainly would use __slots__ to save on memory allocations. Also make it possible to pass in the attribute values when creating the instance:



          class Edge:
          __slots__ = ('node1', 'node2', 'weight')
          def __init__(self, node1=0, node2=0, weight=0):
          self.node1 = node1
          self.node2 = node2
          self.weight = weight


          With the updated __init__ you can use a list comprehension:



          edges = [Edge(*args) for args in ijw]


          Together these can shave off a decent amount of time creating the objects, roughly halve the time needed.



          Comparison creating 1 million objects; the setup:



          >>> from random import randrange
          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 6)]
          >>> class OrigEdge:
          ... def __init__(self):
          ... self.node1 = 0
          ... self.node2 = 0
          ... self.weight = 0
          ...
          >>> origloop = '''
          ... edges=
          ... for (i,j,w) in ijw:
          ... edge = Edge()
          ... edge.node1 = i
          ... edge.node2 = j
          ... edge.weight = w
          ... edges.append(edge)
          ... '''
          >>> class SlotsEdge:
          ... __slots__ = ('node1', 'node2', 'weight')
          ... def __init__(self, node1=0, node2=0, weight=0):
          ... self.node1 = node1
          ... self.node2 = node2
          ... self.weight = weight
          ...
          >>> listcomploop = '''[Edge(*args) for args in ijw]'''


          and the timings:



          >>> from timeit import Timer
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          722.1121070033405
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count) * 1000 # milliseconds
          386.6706900007557


          That's nearly 2 times as fast.



          Increasing the random input list to 10^7 items, and the timing difference holds:



          >>> ijw = [(randrange(100), randrange(100), randrange(1000)) for _ in range(10 ** 7)]
          >>> count, total = Timer(origloop, 'from __main__ import OrigEdge as Edge, ijw').autorange()
          >>> (total / count)
          7.183759553998243
          >>> count, total = Timer(listcomploop, 'from __main__ import SlotsEdge as Edge, ijw').autorange()
          >>> (total / count)
          3.8709938440006226






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 8:53

























          answered Nov 20 at 7:57









          Martijn Pieters

          696k12924052245




          696k12924052245












          • Thanks, I will try and test how much it improves!
            – ted930511
            Nov 20 at 8:05










          • Will dataclasses do anything for instantiation speed?
            – Tomalak
            Nov 20 at 8:08






          • 1




            @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
            – Martijn Pieters
            Nov 20 at 10:08






          • 1




            Thanks for clearing that up!
            – Tomalak
            Nov 20 at 10:17






          • 1




            @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
            – Martijn Pieters
            Nov 20 at 10:29




















          • Thanks, I will try and test how much it improves!
            – ted930511
            Nov 20 at 8:05










          • Will dataclasses do anything for instantiation speed?
            – Tomalak
            Nov 20 at 8:08






          • 1




            @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
            – Martijn Pieters
            Nov 20 at 10:08






          • 1




            Thanks for clearing that up!
            – Tomalak
            Nov 20 at 10:17






          • 1




            @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
            – Martijn Pieters
            Nov 20 at 10:29


















          Thanks, I will try and test how much it improves!
          – ted930511
          Nov 20 at 8:05




          Thanks, I will try and test how much it improves!
          – ted930511
          Nov 20 at 8:05












          Will dataclasses do anything for instantiation speed?
          – Tomalak
          Nov 20 at 8:08




          Will dataclasses do anything for instantiation speed?
          – Tomalak
          Nov 20 at 8:08




          1




          1




          @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
          – Martijn Pieters
          Nov 20 at 10:08




          @Tomalak: your dataclass code confirms my timings. It's the same speed as the slots example (the timings are the same within a margin). I may have misunderstood what your 75% was relative to here; the non-slots dataclass speedup is purely due to the list comprehension, not due to you using a dataclass. If you used my class without __slots__ but with the list comp, you'd get the same timings.
          – Martijn Pieters
          Nov 20 at 10:08




          1




          1




          Thanks for clearing that up!
          – Tomalak
          Nov 20 at 10:17




          Thanks for clearing that up!
          – Tomalak
          Nov 20 at 10:17




          1




          1




          @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
          – Martijn Pieters
          Nov 20 at 10:29






          @martineau: See gist.github.com/mjpieters/a4d01024f50d56210dceefc1acdbd487 for timings and memory footprint.
          – Martijn Pieters
          Nov 20 at 10:29














          up vote
          1
          down vote













          Another option is to skip the Edge class and implement the edges via a table, or adjacency matrix.



          E.g.



          A = create_adjacency_graph(ijw)  # Implement to return a IxJ (sparse?) matrix of weights
          edge_a_weight = A[3, 56]
          edge_b_weight = A[670, 1023]
          # etc...


          This does remove some flexibility though, but should be quite fast both to create and use.






          share|improve this answer





















          • In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
            – ted930511
            Nov 20 at 8:03










          • @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
            – B. M.
            Nov 20 at 8:11















          up vote
          1
          down vote













          Another option is to skip the Edge class and implement the edges via a table, or adjacency matrix.



          E.g.



          A = create_adjacency_graph(ijw)  # Implement to return a IxJ (sparse?) matrix of weights
          edge_a_weight = A[3, 56]
          edge_b_weight = A[670, 1023]
          # etc...


          This does remove some flexibility though, but should be quite fast both to create and use.






          share|improve this answer





















          • In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
            – ted930511
            Nov 20 at 8:03










          • @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
            – B. M.
            Nov 20 at 8:11













          up vote
          1
          down vote










          up vote
          1
          down vote









          Another option is to skip the Edge class and implement the edges via a table, or adjacency matrix.



          E.g.



          A = create_adjacency_graph(ijw)  # Implement to return a IxJ (sparse?) matrix of weights
          edge_a_weight = A[3, 56]
          edge_b_weight = A[670, 1023]
          # etc...


          This does remove some flexibility though, but should be quite fast both to create and use.






          share|improve this answer












          Another option is to skip the Edge class and implement the edges via a table, or adjacency matrix.



          E.g.



          A = create_adjacency_graph(ijw)  # Implement to return a IxJ (sparse?) matrix of weights
          edge_a_weight = A[3, 56]
          edge_b_weight = A[670, 1023]
          # etc...


          This does remove some flexibility though, but should be quite fast both to create and use.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 8:01









          Hannes Ovrén

          13k44663




          13k44663












          • In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
            – ted930511
            Nov 20 at 8:03










          • @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
            – B. M.
            Nov 20 at 8:11


















          • In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
            – ted930511
            Nov 20 at 8:03










          • @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
            – B. M.
            Nov 20 at 8:11
















          In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
          – ted930511
          Nov 20 at 8:03




          In my case, I will pass them into other functions, in order to improve code readability, I prefer using class.
          – ted930511
          Nov 20 at 8:03












          @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
          – B. M.
          Nov 20 at 8:11




          @ted930511 : To run any algorithm on huge data, you must think at more efficient data structure/language, unless it is a prototype.
          – B. M.
          Nov 20 at 8:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388451%2fhow-to-speed-up-python-instance-initialization-for-millions-of-objects%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen