1

Before reading, please read this question

In the linked question I asked the fastest way to create a "list-like object" that contains a lot of variables of a simple datatype. The best answer told me to use the array module and it worked wonderfully! Here's a sample code of how it works:

from array import array
a = array('i', (0,)) * 10 ** 8

But now I have a problem ... I don't want to store just integers ... I want to store my own instances of a class I coded. The class can be a 'Point' class:

class Point:
 def __init__(s, x, y):
  s.x = x
  s.y = y
 def dist_from_origin(s):
  return s.x ** 2 + s.y ** 2

What if I wanted to make an array object containing as default for each index Point(0, 0) ? Is that possible? If not, are there alternatives to the array object that are as fast as it? Please don't include in your answer the fact that I could do one array containing the 'x' values and another one containing the 'y' values since this is not a good practice...

Here's how I tried to solve the problem:

from ctypes import Structure, c_int

class Point(Structure):
  _fields_ = [('x', c_int), ('y', c_int)]

p_arr = (Point * 10 ** 8)() # takes 0.5 sec (slow)

My solution may be simpler in code than a solution with the array module (if it exists) but it is VERY slow. Before answering remember that my goal is to have a FAST solution, and not a beautiful one.

Also, I'm not allowed to use any external modules (modules that need to be installed), like numpy.

ArthurQ
  • 118
  • 1
  • 8
  • 2
    It really sounds like you should be sticking with lists. – user2357112 Apr 21 '18 at 19:50
  • @user2357112 lists are the slowest option. Look at the size of my array... – ArthurQ Apr 21 '18 at 19:50
  • 1
    **this is not a good practice** Why are you concerned with that? You're just trying to be as fast as possible, this sometimes precludes good style. – Barmar Apr 21 '18 at 19:53
  • @Barmar actually I am not that concerned with that... I come from competitive programming and problems of sorting arrays of structs based on a customized condition are frequent ... that is not possible using two separate arrays of integers. If the problem asked to sort them by distance from the origin keeping the x and y coordinates, for instance, using two arrays wouldnt be possible – ArthurQ Apr 21 '18 at 19:55
  • what about an int[][] – Primusa Apr 21 '18 at 20:00
  • @Primusa i'm coding in python not in c++ or any language with this syntax ... and btw I think this is a matrix which has nothing to do with what I am asking – ArthurQ Apr 21 '18 at 20:03
  • Its shorthand for a nested array. Your larger array stores a bunch of integer arrays where the first val is your x and the second is your y – Primusa Apr 21 '18 at 20:04
  • Try actually *using* your data structures instead of just timing the creation, and you'll find that lists are more competitive than you think. (Also, you mention sorting. Sorting in Python without external modules is almost always going to go through a list.) – user2357112 Apr 21 '18 at 20:09
  • @Primusa That may solve my problem with the point class but it wouldnt solve for any struct. Your solution wouldnt be able to solve the case where my struct has different datatypes for instance. – ArthurQ Apr 21 '18 at 20:10
  • @user2357112 What do you mean not timing? In copetitive programmiing problems sometimes the time limit is only one second and 0.5 seconds is already half of it... Also you can sort the `array.array` instances using the 'sorted' function for example. (I don't care if it returns a list, I just want to sort it and go through some of its elements!)... This proves that you don't need extra modules to do that... I think you didnt understand me: I want to create an 'empty' list of my structs (of size 10^8) almost instantly – ArthurQ Apr 21 '18 at 20:19
  • I didn't say anything about not timing; I mean time something where you actually use your sequence instead of just creating it. Even looking at each element once is going to take longer than creating it, and `array.array` has a higher overhead for element access than lists do. – user2357112 Apr 21 '18 at 20:24
  • If you're having time trouble just *creating* your data structure, you're not going to have enough time to *use* it. That suggests you should be trying to figure out how to avoid needing a hundred-million-element sequence in the first place. – user2357112 Apr 21 '18 at 20:32

0 Answers0