Because the procedure itself or the python is too slow? Seeking to explain, opti

Python mice. This code execution time is too long, ask everybody to help have a look what the situation was.
  1. import math,time
  2. testM = []
  3. orgx = []
  4. for i in range(8192):
  5. orgx.append(1000*(math.sin(i/1000)))
  6. time1 = time.localtime()
  7. for i in range(200):
  8. testM.append([])
  9. for j in range(8192*4):
  10. testM[i].append(orgx[j%8192])
  11. print(time.localtime().tm_sec-time1.tm_sec)
This code using the 4S time to complete, and in the C less than half a second time. How to optimize the?

Started by Warner at February 13, 2016 - 9:06 PM

To tell you the truth, seven to four lines of code I did not understand, the other eighth lines there is error, guess what you mean, do not know right.
  1. import math,time
  2. orgx = [1000*(math.sin(i/1000)) for i in range(8192)]
  3. time1 = time.localtime()
  4. temp = [orgx[j%8192] for j in range(8192*4)]
  5. testM = [temp[:] for i in range(200)]
  6. print(time.localtime().tm_sec-time1.tm_sec)

Posted by King at February 22, 2016 - 9:21 PM

Using the cProfile module analysis

Posted by Sebastian at December 16, 2016 - 5:21 PM

For the first time to ask questions, too. Feel shy, has revised its.

Posted by Warner at December 20, 2016 - 5:53 PM

The 2 floor has been updated, the original code is correct, this should be faster.

The average operation time on my computer in less than 0.06 seconds.

Posted by King at December 21, 2016 - 5:57 PM

To replace range with xrange

Posted by Ivan at January 04, 2017 - 6:24 PM

The 2 floor is the solution.
Your 9,10 code repeats 200 times, in fact, as long as 200 copies on the line

Posted by Jill at January 05, 2017 - 8:04 PM

Well, thank you, although it really fast.
But I was in order to verify the assignment speed,
Assignment in reality not duplicate data,
But random things real,
Only one one assignment, can not simply copy.
So the slow is slow.

Posted by Warner at January 06, 2017 - 7:03 PM

Don't make 200 copies, but direct calculation, using it in two seconds, okay?

Posted by King at January 08, 2017 - 7:56 PM

Range into xrange

You can check the difference between range and xrange

Posted by Roderick at January 14, 2017 - 8:29 PM