Python Learning Processing

Source: Internet
Author: User

#-*-Coding:utf-8-*-
__author__ = ' Administrator '
Import bisect
#排序说明: Http://en.wikipedia.org/wiki/insertion_sort
#简化一些操作
#1: Reduce the workload by cutting out external calls
"""
Note: Another part of program complexity is invoking other functions, methods and classes introduced, in general, should try to put the code outside the loop, for nested loops
This is even more important, instead of repeating calculations in a loop to calculate values before the loop starts, the inner loop should remain concise.
"""
#2: Deque also provides append and Pop methods that can be executed at the same speed at both ends of the sequence
#例子2: The following:
From collections Import Defaultdict,namedtuple
Import Profile,stat
Import Sys
s=[(' A ', 1), (' B ', 2), (' C ', 3)]
@profile (' defaultdict ')
def faster ():
D=defaultdict (list)
For k,v in S:
D[k].append (v)

@profile (' Dict ')
def slower ():
d={}
For k,v in S:
D.setdefault (k,[]). Append (v)

Print slower (), faster ()
Print stat[' dict ']
Print stat[' defaultdict ']
Lg=defaultdict (Long)
Print lg[' one ']
Cus=namedtuple (' curs ', ' firsname ')
C=cus (U ' apped ', U ' Zifoe ')
Print C.firsname
#更多说明
"""
Reducing the complexity of the work can be done by the algorithm can be very good processing efficient data structure to store the implementation of the database, that is, when the solution is not obvious, should consider discard and rewrite the problem part, not for performance
The readability of the destruction code!#-*-Coding:utf-8-*-
__author__ = ' Administrator '
Import subprocess as process# Portable method that can be handled like a thread
From processing import Process
#安装它的方法, see: http://blog.csdn.net/imzoer/article/details/8701116
Import OS
def work (): #它提供了Thread类很像Process类, can be any platform
print ' Hay I am aprocess ID%d '%os.getpid ()
Ps=[]
For II in range (4):
P=process (Target=work)
Ps.append (P)
P.start ()

For P in PS:
P.join ()
#当创建了进程时, the memory will be forked, the most efficient process used to make them work after they are created to avoid overhead, and to check their state from the thread, in addition to the memory state being replicated, the process
#类还在其构造程序中提供了额外的args参数, it can also be used to pass the required data
#pyprocessing还可以了一个类似于队列的类 can be used to allow multiple processes to share data in a shared memory space managed by the package.
"""
It also provides the ability to share objects between CType and processes, see: http://pyprocessing.berlios.de/doc/shardctypes.html
"""
#例子2
Import Queue as Q
print ' This machine has%d CPUs ' \
%processing.cpucount ()
Def wo ():
F=q.get_nowait ()
Return ' workd on ' +f
Q=processing. Queue ()
Pool=processing. Pool ()
For I in (' F1 ', ' F2 ', ' F3 ', ' F4 ', ' F5 '):
Q.put (i)

While True:
Try
Resut=pool.apply_async (wo)
Print Resut.get (timeout=1)
Except Q.empty:
Break
"""
The Apply_async () method calls the WO function through the process pool and returns the resulting object immediately, which the main process can return as a result
The Get method can be used to wait for the result (with walk-in Settings)
The last array class and the value class provide shared memory space. However, you should avoid using them by design because they can lead to code complexity.
"""


"""

Python Learning Processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.