Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is faster: Python3's 'len' or numpys shape?

I have an array of coordinates:

points = [x,y]

with the (numpy) dimensions/shape: (18, 1, 2)

In matlab, to initialize an array of ones to index these points with '3', I could do this:

A = ones(size(points,1),1)*3'

How could I do this using python3 and numpy in the fastest possible way?

like image 312
Peter Turner Avatar asked Oct 15 '25 04:10

Peter Turner


1 Answers

    def time_compare(self):
        loops = 100000000
        start = time.time()
        for i in range(loops):
            self.value_map.shape[0]
            self.value_map[0].shape[0]
            self.value_map[0][0].shape[0]
            self.value_map[0][0][0].shape[0]
        end = time.time()
        timed = (end - start)
        print("shape={}".format(timed))

        start = time.time()
        for i in range(loops):
            len(self.value_map)
            len(self.value_map[0])
            len(self.value_map[0][0])
            len(self.value_map[0][0][0])
        end = time.time()
        timed = (end - start)
        print("len={}".format(timed))

shape=102.26551818847656
len=87.99720764160156

len is faster than shape

like image 159
Rusty Robot Avatar answered Oct 17 '25 19:10

Rusty Robot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!