-5

I've gone through the Convert all strings in a list to int post

I want to convert

results = ['1', '2', '3']

to:

results = [1, 2, 3]

I know i can do it by

  1. map(int, results)

and

  1. results = list(map(int, results))

I want it faster way may be using numpy or more faster.

Actual code is

from sys import stdin, stdout
import numpy as np
n = int(stdin.readline())


for i in range(0,n):
    lone = 0
    m = int(stdin.readline())
    results = stdin.readline().split()
    o = np.array(results, dtype=np.int64)
    for j in range (0,m):
        if o[j] in  o[j+1:m]:
            lone = lone +1
        elif o[j] in o [0:j]:
            lone = lone +1
        else:
            stdout.write(str(o[j]) + '\n')

    if lone == m:
        stdout.write ("-1 \n")

Please let me know if there is any methods to achieve this when trying to work with thousands of strings

Abhishek Parikh
  • 949
  • 1
  • 20
  • 41

1 Answers1

2

Multiprocessing is a way to get it done faster (in addition of using Numpy):

E.g:

In [11]: from multiprocessing import Pool

In [12]: pool = Pool(10)

In [13]: pool.map(int, [str(i) for i in range(500)])

Numpy will mostly provide a memory gain as you would be dealing with primitive types instead of python objects, but will also provide a non-negligible speed gain as well. Optimizing time of an iteration like this is always done by using parallelization so I advise using both Numpy and a process pool.

Maresh
  • 4,644
  • 25
  • 30
  • 2
    No way is this going to be faster than anything numpy, unless you probably combine numpy with this somehow, and even then you'll only see differences over 1M elements, possibly more. Multiprocessing simply presents too much overhead to be useful here. – cs95 Sep 21 '17 at 10:45
  • 1
    I'm not saying that Python objects will be faster that numpy primitives. I'm saying that iterations are best optimized with parallelization. Best to use both anyway. And for a very very long list the pool will win for sure. I'll edit to reflect that thought. – Maresh Sep 21 '17 at 10:48