def acquire(self, blocking=True, timeout=-1):
"""Must be used with 'yield' as 'yield lock.acquire()'.
"""
if not blocking and self._owner is not None:
raise StopIteration(False)
if not self._scheduler:
self._scheduler = Pycos.scheduler()
task = Pycos.cur_task(self._scheduler)
if timeout < 0:
timeout = None
while self._owner is not None:
if timeout is not None:
if timeout <= 0:
raise StopIteration(False)
start = _time()
self._waitlist.append(task)
if (yield task._await_(timeout)) is None:
try:
self._waitlist.remove(task)
except ValueError:
pass
if timeout is not None:
timeout -= (_time() - start)
self._owner = task
raise StopIteration(True)
python类clock()的实例源码
def SendMessage(self, message, main_thread = True):
global connections, in_pipe, out_pipe
message_sent = False
now = time.clock()
if message['message'] == 'closed':
message['time'] = now
else:
message['time'] = time.clock() + self.latency
message['size'] = .0
if 'data' in message:
message['size'] = float(len(message['data']))
try:
connection_id = message['connection']
# Send messages directly, bypassing the queues is throttling is disabled and we are on the main thread
if main_thread and connection_id in connections and self.peer in connections[connection_id]and self.latency == 0 and self.kbps == .0:
message_sent = self.SendPeerMessage(message)
except:
pass
if not message_sent:
try:
self.queue.put(message)
except:
pass
def SendPeerMessage(self, message):
global last_activity
last_activity = time.clock()
message_sent = False
connection_id = message['connection']
if connection_id in connections:
if self.peer in connections[connection_id]:
try:
connections[connection_id][self.peer].handle_message(message)
message_sent = True
except:
# Clean up any disconnected connections
try:
connections[connection_id]['server'].close()
except:
pass
try:
connections[connection_id]['client'].close()
except:
pass
del connections[connection_id]
return message_sent
def trace_dispatch(self, frame, event, arg):
timer = self.timer
t = timer()
t = t[0] + t[1] - self.t - self.bias
if event == "c_call":
self.c_func_name = arg.__name__
if self.dispatch[event](self, frame,t):
t = timer()
self.t = t[0] + t[1]
else:
r = timer()
self.t = r[0] + r[1] - t # put back unrecorded delta
# Dispatch routine for best timer program (return = scalar, fastest if
# an integer but float works too -- and time.clock() relies on that).
def process_sample(self, signal_frame, sample_time, main_thread_id):
if self.profile:
start = time.clock()
current_frames = sys._current_frames()
items = current_frames.items()
for thread_id, thread_frame in items:
if thread_id == main_thread_id:
thread_frame = signal_frame
stack = self.recover_stack(thread_frame)
if stack:
current_node = self.profile
for frame in reversed(stack):
current_node = current_node.find_or_add_child(str(frame))
current_node.increment(sample_time, 1)
thread_id, thread_frame, stack = None, None, None
items = None
current_frames = None
self.profile._overhead += (time.clock() - start)
def build_graph():
# Build GUI environment.
global frame_handle, y, x, start, sec, timer_text, clock_handle
frame_handle = graph.after(1000 / FRAMES_PER_SEC, update)
graph.bind('<1>', change)
graph['background'] = GAME_COLOR
# Draw environment.
y = HEIGHT - WALL + BALL_RADIUS + 2
graph.create_rectangle((0, 0, WALL - BALL_RADIUS, y), fill=FORCE_COLOR)
graph.create_rectangle((WIDTH - WALL + BALL_RADIUS, 0, WIDTH, y), fill=FORCE_COLOR)
graph.create_line((0, y, WIDTH, y), fill=FLOOR_COLOR, width=3)
# Prepare timer data.
x = (WALL - BALL_RADIUS) / 2
y = (y + HEIGHT) / 2
start = time.clock()
sec = 0
timer_text = graph.create_text(x, y, text=f_time(TIME_LIMIT))
clock_handle = graph.after(1000, update_clock)
################################################################################
# ANIMATION LOOP FUNCTIONS
def update():
'Run physics and update screen.'
global frame
try:
for mutate in wall, floor, gravity, friction, governor:
for ball in balls:
mutate(ball)
for index, ball_1 in enumerate(balls[:-1]):
for ball_2 in balls[index+1:]:
ball_1.crash(ball_2)
for ball in balls:
ball.move(FPS)
screen.delete('animate')
for ball in balls:
x1 = ball.pos.x - ball.rad
y1 = ball.pos.y - ball.rad
x2 = ball.pos.x + ball.rad
y2 = ball.pos.y + ball.rad
screen.create_oval(x1, y1, x2, y2, fill=BALL_COLOR, tag='animate')
frame += 1
screen.after(int((start + frame / FPS - time.clock()) * 1000), update)
except:
screen.delete(Tkinter.ALL)
screen.create_text(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, text=traceback.format_exc(), font='Courier 10', fill='red', tag='animate')
def print_status(progress, file_size, start):
"""
This function - when passed as `on_progress` to `Video.download` - prints
out the current download progress.
:params progress:
The lenght of the currently downloaded bytes.
:params file_size:
The total size of the video.
:params start:
The time when started
"""
percent_done = int(progress) * 100. / file_size
done = int(50 * progress / int(file_size))
dt = (clock() - start)
if dt > 0:
stdout.write("\r [%s%s][%3.2f%%] %s at %s/s " %
('=' * done, ' ' * (50 - done), percent_done,
sizeof(file_size), sizeof(progress // dt)))
stdout.flush()
def score(train_labels, train_features, test_labels, test_features, save_file, use_tree=False):
if use_tree:
train_clf = Classifier(tree.DecisionTreeClassifier())
else:
train_clf = Classifier()
print train_clf.clf
print ''
t_start = time.clock()
train_clf.learn(train_features, train_labels)
t_end = time.clock()
if save_file:
train_clf.save_to_file(open(save_file, 'w'))
p_start = time.clock()
predicted = train_clf.clf.predict(test_features)
p_end = time.clock()
test_labels_t = train_clf.labels.transform(test_labels)
print classification_report(test_labels_t, predicted, target_names=train_clf.labels.classes_)
print 'Training time: %fs' % (t_end - t_start)
print 'Predicting time: %fs' % (p_end - p_start)
print 'Mean squared error: %f' % mean_squared_error(test_labels_t, predicted)
return train_clf.score(test_features, test_labels)
def _create_check_retry(self, **kwargs):
"""
Check the mon service is started and responding with time out.
On heavily overloaded hardware it can takes a while for the mon service
to start
"""
# Number of seconds before a time out.
timeout = 60
time_start = time.clock()
time_end = time_start + timeout
if self._create_check_responding(**kwargs):
return True
while time.clock() < time_end:
log.info("Mon service did not start up, waiting.")
time.sleep(5)
log.info("Retrying mon service.")
if self._create_check_responding(**kwargs):
return True
log.error("Timed out starting mon service")
raise Error("Failed to get mon service status after '%s' seconds." % (timeout))
def __call__(self, func, *args, **kwargs):
'''Used to process callbacks in throughput-limiting thread
through queue.
Args:
func (:obj:`callable`): the actual function (or any callable) that
is processed through queue.
*args: variable-length `func` arguments.
**kwargs: arbitrary keyword-arguments to `func`.
Returns:
None
'''
if not self.is_alive() or self.__exit_req:
raise DelayQueueError('Could not process callback in stopped thread')
self._queue.put((func, args, kwargs))
# The most straightforward way to implement this is to use 2 sequenital delay
# queues, like on classic delay chain schematics in electronics.
# So, message path is:
# msg --> group delay if group msg, else no delay --> normal msg delay --> out
# This way OS threading scheduler cares of timings accuracy.
# (see time.time, time.clock, time.perf_counter, time.sleep @ docs.python.org)
def validate(beam_searcher, dataset, logger=None, res_file=None):
if logger is None:
logger = Logger(None)
# generating captions
all_candidates = []
tic = time.clock()
for i in xrange(dataset.n_image):
data = dataset.iterate_batch() # data: id, img, scene...
sent = beam_searcher.generate(data[1:])
cap = ' '.join([dataset.vocab[word] for word in sent])
print '[{}], id={}, \t\t {}'.format(i, data[0], cap)
all_candidates.append({'image_id': data[0], 'caption': cap})
toc = time.clock() - tic
running_time = toc / 5000.0
if res_file is None:
res_file = 'tmp.json'
json.dump(all_candidates, open(res_file, 'w'))
gt_file = osp.join(dataset.data_dir, 'captions_'+dataset.data_split+'.json')
scores = evaluate(gt_file, res_file, logger)
if res_file == 'tmp.json':
os.system('rm -rf %s' % res_file)
return scores, running_time
def trace_dispatch(self, frame, event, arg):
timer = self.timer
t = timer()
t = t[0] + t[1] - self.t - self.bias
if event == "c_call":
self.c_func_name = arg.__name__
if self.dispatch[event](self, frame,t):
t = timer()
self.t = t[0] + t[1]
else:
r = timer()
self.t = r[0] + r[1] - t # put back unrecorded delta
# Dispatch routine for best timer program (return = scalar, fastest if
# an integer but float works too -- and time.clock() relies on that).
def calc_latency(self, node_to_calc):
""" calculate latency of given node, remove if node is not connected """
start = time.clock()
success = False
try:
for i in range(5):
node_to_calc.client.ping()
node_to_calc.latency = ((time.clock() - start) / 5) * 1000 # converting to ms
success = True
except: # node not connected
print(str(sys.exc_info()))
logger().warning("error attempting to ping an unregistered node: disconnecting node")
if node_to_calc in self.connections: # if a registered node disconnects
self.connections.remove(node_to_calc)
self.remove_from_peer_dict(node_to_calc)
try:
net_dao.update_failed_ping(node_to_calc)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
logger().warning(message)
return success
def extract(sentences, ignore_entities=get_ignore_entities()):
tic = time.clock()
nlp = pycorenlp.corenlp.StanfordCoreNLP("http://{0}:{1}/".format(get_ner_host(), get_ner_port()))
extraction = []
for s in sentences:
output = nlp.annotate(s, properties={"annotators": "ner", "outputFormat": "json"})
locations_found = [(t['originalText']) for t in output["sentences"][0]["tokens"] for item in output if
t['ner'] in ['LOCATION', 'PERSON'] and t[
'originalText'].lower() not in ignore_entities]
if len(locations_found) > 0:
extraction.append(({'text': s, 'entities': locations_found}))
tac = time.clock()
logger.info('NER extraction took {time}ms'.format(time=tac - tic))
return extraction
# Perform natural language processing to text, get annotated entities and entities relations
def extract_ner(sentences, ignore_entities=get_ignore_entities()):
try:
tagger = Ner(host=get_ner_host(), port=get_ner_port())
tic = time.clock()
extraction = []
for s, file in sentences:
output = tagger.get_entities(s.replace('\n', ' ').replace('\r', ''))
locations_found = [text for text, tag in output if
tag in ['LOCATION', 'PERSON'] and text.lower() not in ignore_entities]
if len(locations_found) > 0:
extraction.append(({'text': {'text': s, 'file': file}, 'entities': locations_found}))
tac = time.clock()
logger.info('NER extraction took {time}ms'.format(time=tac - tic))
return extraction
except Exception as detail:
logger.error('Error during ner extraction {}'.format(detail))
raise
def main():
startTime = time.clock()
start = getMaxAvId()
if start == None: # ????????av?
start = 1
# print(start)
print ("av start: ", start)
stop = int(input("av stop: "))
# print(123)
for i in range(start+1, stop+1):
saveDanmu(i)
# print(i)
stopTime = time.clock()
print ((stopTime - startTime)/60,)
print ("mins")
def download_file(self) -> None:
req = requests.get(self.download_link, stream=True, proxies=self.proxy)
filesize = int(req.headers['Content-Length'])
filename = os.path.basename(self.download_path)
downloadedChunk = 0
blockSize = 8192
start = time.clock()
with open(self.download_path, 'wb') as f:
for chunk in req.iter_content(chunk_size=blockSize):
if self.cancel_download or not chunk:
req.close()
break
f.write(chunk)
downloadedChunk += len(chunk)
progress = float(downloadedChunk) / filesize
self.dlProgress.emit(progress * 100)
dlspeed = downloadedChunk//(time.clock() - start) / 1000
progressTxt = '<b>Downloading {0}</b>:<br/>{1} of <b>{3}</b> [{2:.2%}] [{4} kbps]' \
.format(filename, downloadedChunk, progress, size(filesize, system=alternative), dlspeed)
self.dlProgressTxt.emit(progressTxt)
self.dlComplete.emit()
def nextFrame(self, arg):
# (arg is the frame number, which we don't need)
self.frame_c += 1
print("computed frame:" + str(self.frame_c))
if WindSim.performanceData and (arg % 30 == 0) and (arg > 0):
endTime = clock()
print ("Took {0} seconds".format(endTime - self.startTime))
print ("%1.1f" % (30 / (endTime - self.startTime)),
'frames per second')
self.startTime = endTime
if (WindSim.save):
frameName = 'output/frame%04d.png' % arg
plt.savefig(frameName)
self.frameList.write(frameName + '\n')
for step in range(WindSim.step_range):
self.stream()
self.collide()
self.fluidImage.set_array(self.curl(WindSim.ux, WindSim.uy))
return (self.fluidImage, self.barrierImage)
def TestConnection(dbname):
# Create the ADO connection object, and link the event
# handlers into it
c = DispatchWithEvents("ADODB.Connection", ADOEvents)
# Initiate the asynchronous open
dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=%s" % dbname
user = "system"
pw = "manager"
c.Open(dsn, user, pw, constants.adAsyncConnect)
# Sit in a loop, until our event handler (above) sets the
# "finished" flag or we time out.
end_time = time.clock() + 10
while time.clock() < end_time:
# Pump messages so that COM gets a look in
pythoncom.PumpWaitingMessages()
if not finished:
print "XXX - Failed to connect!"
def WaitWhileProcessingMessages(event, timeout = 2):
start = time.clock()
while True:
# Wake 4 times a second - we can't just specify the
# full timeout here, as then it would reset for every
# message we process.
rc = win32event.MsgWaitForMultipleObjects( (event,), 0,
250,
win32event.QS_ALLEVENTS)
if rc == win32event.WAIT_OBJECT_0:
# event signalled - stop now!
return True
if (time.clock() - start) > timeout:
# Timeout expired.
return False
# must be a message.
pythoncom.PumpWaitingMessages()
def WaitUntil(predicate, timeout_seconds=1):
"""Blocks until the provided predicate (function) is true.
Returns:
Whether the provided predicate was satisfied once (before the timeout).
"""
start_time = time.clock()
sleep_time_sec = 0.025
while True:
if predicate():
return True
if time.clock() - start_time > timeout_seconds:
return False
time.sleep(sleep_time_sec)
sleep_time_sec = min(1, sleep_time_sec * 2) # Don't wait more than 1 sec.
# Implementation of chrome_test_server_spawner.PortForwarder that doesn't
# forward ports. Instead the tests are expected to connect to the host IP
# address inside the virtual network provided by qemu. qemu will forward
# these connections to the corresponding localhost ports.
def __init__(self, video_src = 0,
interactive = True,
video = 'chess.avi', fallback = 'synth:bg=./data/hi.jpg:noise=0.05', nFrames = 500):
cam = create_capture(video_src, fallback=fallback)
if not cam:
print("Problem initialising cam")
vwriter = VideoWriter(video)
run = True
t = clock()
frameCounter = nFrames
while frameCounter>0:
ret, img = cam.read()
vwriter.addFrame(img, width=1920)
frameCounter-=1
print("%d" % frameCounter)
print("Created chessboard video : saved %d frames to %s" % (nFrames, video))
vwriter.finalise()
#####################################################################################################################
# Tutorials
#####################################################################################################################
# Placeholder for snippets used in development taken from the tutorials.
def seed(self, seed=None):
"""Resets the state of the random number generator with a seed.
.. seealso::
:func:`cupy.random.seed` for full documentation,
:meth:`numpy.random.RandomState.seed`
"""
if seed is None:
try:
seed_str = binascii.hexlify(os.urandom(8))
seed = numpy.uint64(int(seed_str, 16))
except NotImplementedError:
seed = numpy.uint64(time.clock() * 1000000)
else:
seed = numpy.asarray(seed).astype(numpy.uint64, casting='safe')
curand.setPseudoRandomGeneratorSeed(self._generator, seed)
curand.setGeneratorOffset(self._generator, 0)
def seed(seed=None):
"""Resets the state of the random number generator with a seed.
This function resets the state of the global random number generator for
the current device. Be careful that generators for other devices are not
affected.
Args:
seed (None or int): Seed for the random number generator. If ``None``,
it uses :func:`os.urandom` if available or :func:`time.clock`
otherwise. Note that this function does not support seeding by an
integer array.
"""
get_random_state().seed(seed)
# CuPy specific functions
def flatwrite(me, uvs, matimage = False) :
#t = clock()
newuvs = []
#print('uv funcinput : %s'%(len(uvs)))
# uvi : uvlayer id uvlist : uv coordinates list
for uvi, uvlist in enumerate(uvs) :
#print('uvlist input : %s'%(len(uvlist)))
#print(uvlist[0:5])
uv = me.uv_textures.new()
uv.name = 'UV%s'%uvi
uvlayer = me.uv_layers[-1].data
# flatuv = awaited uvlist length
#flatuv = list( range(len(uvlayer) * 2) )
#print('uvlist need : %s'%(len(flatuv)))
uvlayer.foreach_set('uv',uvlist)
newuvs.append(uv)
#print('uvs in ',clock() - t)
return newuvs
# face are squared or rectangular,
# any orientation
# vert order width then height 01 and 23 = x 12 and 03 = y
# normal default when face has been built
def BENCH():
debug_prints(func="BENCH", text="BEGIN BENCHMARK")
bt0 = time.clock()
# make a big list
tsize = 25
tlist = []
for x in range(tsize):
for y in range(tsize):
for z in range(tsize):
tlist.append((x, y, z))
tlist.append((x, y, z))
# function to test
bt1 = time.clock()
bt2 = time.clock()
btRUNb = bt2 - bt1
btRUNa = bt1 - bt0
debug_prints(func="BENCH", text="SETUP TIME", var=btRUNa)
debug_prints(func="BENCH", text="BENCHMARK TIME", var=btRUNb)
debug_print_vars(
"\n[BENCH]\n",
"GRIDSIZE: ", tsize, ' - ', tsize * tsize * tsize
)
def _ingest_pairs(self, pairs, oid2nid, frame_size, limit, single_sided):
oid2nid_v = np.vectorize(oid2nid.get)
# whole pairs set does not fit in memory, so split it in frames with `frame_size` number of pairs.
for start in range(0, limit, frame_size):
stop = frame_size + start
t1 = process_time()
six.print_('Fetching pairs {0}:{1} of {2} ... '.format(start, stop, limit), end='', flush=True)
raw_frame = pairs.read(start=start, stop=stop)
t2 = process_time()
six.print_('{0}s, Parsing ... '.format(int(t2 - t1)), flush=True)
frame = self._translate_frame(raw_frame, oid2nid_v, single_sided)
t3 = process_time()
six.print_('Writing ... '.format(int(t3 - t2)), flush=True)
# alternate direction, to make use of cached chunks of prev frame
self._ingest_pairs_frame(frame)
del frame
t4 = process_time()
six.print_('{0}s, Done with {1}:{2} in {3}s'.format(int(t4 - t3), start, stop, int(t4 - t1)), flush=True)
Similarity Metrics - In Parallel.py 文件源码
项目:Parallel-Processing-Nadig
作者: madhug-nadig
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def parallel_cosine_similarity(self,x,y):
pool = mp.Pool(processes= 16)
s = time.clock()
nums = pool.starmap(self.multplierr, zip(x,y))
numerator = sum(nums)
#x_sqr = pool.starmap( self.multplierr, zip(x,x))
#y_sqr = pool.starmap( self.multplierr, zip(y,y))
#denominator = round(sqrt(sum(x_sqr))) * round(sqrt(sum(y_sqr)))
denominator = self.square_rooted(x)*self.square_rooted(y)
e = time.clock()
print("Parallel Cosine Exec Time: ", e-s)
return round(numerator/float(denominator),3)
#JACCARD SIMILARITY
#Serial Jaccard Similarity
Similarity Metrics - In Parallel.py 文件源码
项目:Parallel-Processing-Nadig
作者: madhug-nadig
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def parallel_jaccard_similarity(self,x,y):
p = 16
pool = mp.Pool(processes= p)
chunk_X = []
chunk_Y = []
for i in range(0, len(x), p):
chunk_X.append(x[int(i):int((i+1)*p)])
chunk_Y.append(y[int(i):int((i+1)*p)])
s = time.clock()
intersection_cardinality = sum(pool.starmap(self.interc_card_locl, zip(chunk_X,chunk_Y)))
union_cardinality = sum(pool.starmap(self.union_card_locl, zip(chunk_X,chunk_Y)))
print(intersection_cardinality, union_cardinality)
e = time.clock()
print("Parallel Jaccard Exec Time: ", e-s)
return intersection_cardinality/float(union_cardinality)