Python time.clock() Examples

The following are code examples for showing how to use time.clock(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: god-eye   Author: PiScale   File: download_speed.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __call__(self, client, dnode):
        logger.info('Test download speed :  running...')
        start = time.clock()
        r = requests.get('http://{}'.format(dnode), stream=True)
        total_length = int(r.headers.get('content-length'))
        if total_length is None:
            logger.error("Empty file!")
        else:
            array_speed = []
            start_chunk = time.clock()
            for chunk in r.iter_content(1024):  # 1kB1024 1MB 1048576
                end_chunk = time.clock()
                delta = end_chunk - start_chunk
                start_chunk = end_chunk
                if delta <= 0:
                    break
                else:
                    array_speed.append(1//delta)  # kB / s

            end = time.clock()
            yield from self._queue.put(self.get_result(dnode, start, end, total_length, array_speed)) 
Example 2
Project: god-eye   Author: PiScale   File: download_speed.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_result(self, url, start, end, total_length, array_speed):
        """Download and processing data.

        Args:
            url (str): url file download.
            start (float):  It's time which started download.
            end (float): It's time which finished download.
            total_length (int): size of file download (Byte)
            array_speed (list): list download speeds for each 1024 Byte (kB/s)

        Returns:
            list with item 0 : json format for influxdb
        """

        download_speed = total_length // (time.clock() - start)
        accelerationS = self.acceleration(array_speed)
        mean_deviationS = self.mean_deviation(array_speed, download_speed)
        logger.info("Test download speed done!")
        #TODO Bỏ time, để kiểm tra xem db có ghi đc dữ liệu hay chưa
        return [self.output([self._snode, url, datetime.now(), download_speed, mean_deviationS, accelerationS])] 
Example 3
Project: pyblish-win   Author: pyblish   File: profile.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
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). 
Example 4
Project: elsbot   Author: Praisebetoscience   File: archives.py    MIT License 6 votes vote down vote up
def ratelimit(max_per_second):
    min_interval = 1.0 / float(max_per_second)

    def decorate(func):
        last_time_called = [0.0]

        def rate_limited_func(*args, **kargs):
            elapsed = time.clock() - last_time_called[0]
            left_to_wait = min_interval - elapsed
            if left_to_wait > 0:
                time.sleep(left_to_wait)
            ret = func(*args, **kargs)
            last_time_called[0] = time.clock()
            return ret
        return rate_limited_func
    return decorate 
Example 5
Project: ARPET   Author: juliagarriga   File: tracker.py    MIT License 6 votes vote down vote up
def __init__(self, window_name, drone, net, frames, vmax, w, h, kp, ki, kd, tol, alpha):
        
        self.window_name = window_name
        self.net = net
        self.tol = tol
        self.frames = frames
        self.alpha = alpha
        self.kalman = self.init_kalman_filter(4, 4)
        self.h = h

        t = str(time.clock())

        # Files used to create the Kalman filter graphics
        self.file_tracker = open("textfiles/file_tracker" + t + ".txt", "w")
        self.file_time = open("textfiles/file_time" + t + ".txt", "w")
        self.file_kalman = open("textfiles/file_kalman" + t + ".txt", "w")

        pidx = pid.PID(vmax, w, h, kp[0], ki[0], kd[0])
        pidy = pid.PID(vmax, w, h, kp[1], ki[1], kd[1])
        pidz = pid.PID(vmax, w, h, kp[2], ki[2], kd[2])
        pidt = pid.PID(vmax, w, h, kp[3], ki[3], kd[3])

        self.controller = move_drone.MoveDrone(drone, pidx, pidy, pidz, pidt) 
Example 6
Project: BayesRate   Author: schnitzler-j   File: BayesRateTDIthreads.py    MIT License 6 votes vote down vote up
def startMCMC(i,thread):
	global bd, exp_M, rj, categ
	t1 = time.clock()
	r.append(MCMC(K,i, runs, iteration, burnin, categ, rep, mod_times, mod_rates, rj, sample_freq, rand_tree, bd, exp_M, model,\
	lam_rA, lam_mA, path_dir, name_file, fixed_times, prior_shift, sample_fraction, print_freq, fix_k, sequential, BETA))
	if i==0: print "\n MCMC execution time: {0:.2f} min".format((time.clock()-t1)/60.)
	if runs>1 and i==0: print " finishing remote chains..." 
	
	if len(r)==runs:
		out_list=list()
		for i in range(0, runs): 
			if bd==1: MM="BD"
			if bd==0: MM="PB%s" % (categ)
			out_file = "%sBFlogs/%s_sum_%s_%s.txt" % (path_dir,name_file, MM, i)
			#out_file="%s_%s" % (name_file, i)
			out_list.append(out_file)
		#print " results saved in files: %s\n" % (out_list)
		print " summarizing %s runs (%s trees)..." % (runs, rep)
		import mcmc_tdi
		temperatures=mcmc_tdi.temp_gen(BETA, runs)
		marginal_likelihood(runs, out_list, path_dir, name_file, MM, temperatures)


		print "\n Use 'Ctrl-C' to quit.\n" 
Example 7
Project: attack-graphs   Author: cyberImperial   File: benchmarks.py    MIT License 6 votes vote down vote up
def build_scenario(name, nodes, edges, slaves, snaps, pause, processor, batch_threads):
    graph = generate_graph(nodes, edges)
    export_graph(graph, name)

    create_master(name, batch_threads)
    for i in range(slaves):
        time.sleep(2)
        create_slave(name, 1000 + i, batch_threads)

    snapshots = [(time.clock(), graph)]
    for i in range(0, snaps):
        time.sleep(pause)
        snapshot = take_snapshot()
        if snapshot is not None:
            snapshots.append(snapshot)
    delete_simulation_config(name)
    nodes = process_snapshots(snapshots, processor)

    clear_processes()
    return nodes 
Example 8
Project: pyspark-cassandra   Author: TargetHolding   File: tests.py    Apache License 2.0 6 votes vote down vote up
def test_write_conf(self):
        rdd = self.sc.parallelize([{'key':i, 'text':i, 'int':i} for i in range(10)])
        save = partial(rdd.saveToCassandra, self.keyspace, self.table)

        save(batch_size=100)
        save(batch_buffer_size=100)
        save(batch_grouping_key='replica_set')
        save(batch_grouping_key='partition')
        save(consistency_level='ALL')
        save(consistency_level=ConsistencyLevel.LOCAL_QUORUM)
        save(parallelism_level=10)
        save(throughput_mibps=10)
        save(ttl=5)
        save(ttl=timedelta(minutes=30))
        save(timestamp=time.clock() * 1000 * 1000)
        save(timestamp=datetime.now())
        save(metrics_enabled=True)
        save(write_conf=WriteConf(ttl=3, metrics_enabled=True)) 
Example 9
Project: kaggle-carvana-2017   Author: killthekitten   File: generate_encoded_submission.py    MIT License 6 votes vote down vote up
def encoder(in_queue, threshold, generated_masks, time_counts):
    while True:
        img_name, mask_img_path = in_queue.get()

        if img_name is None:
            break

        t0 = time.clock()
        mask_img = ndimage.imread(mask_img_path, mode='L')
        mask_img[mask_img <= threshold] = 0
        mask_img[mask_img > threshold] = 1
        time_counts['time_read'].append(time.clock() - t0)

        t0 = time.clock()
        rle = rle_encode(mask_img)
        time_counts['time_rle'].append(time.clock() - t0)

        t0 = time.clock()
        rle_string = rle_to_string(rle)
        time_counts['time_stringify'].append(time.clock() - t0)

        generated_masks.append((img_name, rle_string)) 
Example 10
Project: razzy-spinner   Author: rafasashi   File: featurechart.py    GNU General Public License v3.0 6 votes vote down vote up
def demo(print_times=True, print_grammar=True,
         print_trees=True, print_sentence=True,
         trace=1,
         parser=FeatureChartParser,
         sent='I saw John with a dog with my cookie'):
    import sys, time
    print()
    grammar = demo_grammar()
    if print_grammar:
        print(grammar)
        print()
    print("*", parser.__name__)
    if print_sentence:
        print("Sentence:", sent)
    tokens = sent.split()
    t = time.clock()
    cp = parser(grammar, trace=trace)
    chart = cp.chart_parse(tokens)
    trees = list(chart.parses(grammar.start()))
    if print_times:
        print("Time: %s" % (time.clock() - t))
    if print_trees:
        for tree in trees: print(tree)
    else:
        print("Nr trees:", len(trees)) 
Example 11
Project: robot-navigation   Author: ronaldahmed   File: demo.py    MIT License 6 votes vote down vote up
def demo(best_path, cache_factory):
    # demonstrates POS tagging using supervised training

    print 'Training HMM...'
    labelled_sequences, tag_set, symbols = load_pos()
    trainer = HiddenMarkovModelTrainer(tag_set, symbols)
    hmm = trainer.train_supervised(Token(SUBTOKENS=labelled_sequences[100:]),
                    estimator=lambda fd, bins: LidstoneProbDist(fd, 0.1, bins))

    print 'Creating cache', cache_factory
    cache = cache_factory(hmm)

    print 'Overriding best_path with', best_path
    hmm.__class__.best_path = lambda self, seq: best_path(self, seq, cache)

    print 'Testing...'
    import time
    start = time.clock()
    test_pos(hmm, labelled_sequences[:100], True)
    print 'elapsed time', (time.clock() - start) 
Example 12
Project: segment_lane_lines   Author: chicagoedt   File: lane_segmentation_node.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __segment_and_publish(self, img):
        """Runs the segmentation model and publishes the result.

        Args:
            data (sensor_msgs/Image): The ROS Image message, exactly as passed
                by the subscriber to its callback.

        Returns:
            A NumPy array representing the segmented image.

        """
        start = time.clock()
        # Preprocess input.
        model_input = np.array([img]).astype('float32') / 255
        # Run the semantic segmentation model.
        with tf.get_default_graph().as_default():
            lane_lines = (self.__model.predict(model_input)[0] * 255).astype(np.uint8)
        # Publish and return.
        lane_lines_rgb = cv2.cvtColor(lane_lines, cv2.COLOR_GRAY2RGB)
        self.__image_pub.publish(self.numpy_to_msg(lane_lines_rgb))
        end = time.clock()
        print("Latency: " + str((end - start) * 1000) + " milliseconds.")

        return lane_lines 
Example 13
Project: OpenBottle   Author: xiaozhuchacha   File: featurechart.py    MIT License 6 votes vote down vote up
def demo(print_times=True, print_grammar=True,
         print_trees=True, print_sentence=True,
         trace=1,
         parser=FeatureChartParser,
         sent='I saw John with a dog with my cookie'):
    import sys, time
    print()
    grammar = demo_grammar()
    if print_grammar:
        print(grammar)
        print()
    print("*", parser.__name__)
    if print_sentence:
        print("Sentence:", sent)
    tokens = sent.split()
    t = time.clock()
    cp = parser(grammar, trace=trace)
    chart = cp.chart_parse(tokens)
    trees = list(chart.parses(grammar.start()))
    if print_times:
        print("Time: %s" % (time.clock() - t))
    if print_trees:
        for tree in trees: print(tree)
    else:
        print("Nr trees:", len(trees)) 
Example 14
Project: god-eye   Author: PiScale   File: time_to_first_byte.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __call__(self, client, dnode):
        logger.info('Caculating time for download first byte...')
        r = requests.get('http://{}'.format(dnode), stream=True)
        total_length = int(r.headers.get('content-length'))
        if total_length is None:
            logger.info("empty file!")
        else:
            start_chunk = time.clock()
            for chunk in r.iter_content(1024):  # 1kB1024 1MB 1048576
                end_chunk = time.clock()
                break

            delta = end_chunk - start_chunk  # time to first byte
            yield from self._queue.put(self.get_result(dnode, delta)) 
Example 15
Project: pyblish-win   Author: pyblish   File: profile.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _get_time_times(timer=os.times):
        t = timer()
        return t[0] + t[1]

# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min. 
Example 16
Project: pyblish-win   Author: pyblish   File: test_numbers.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def run_test(rep, msg, func, arg=None):
##    items = [None] * rep
    items = range(rep)
    from time import clock
    if arg is not None:
        start = clock()
        for i in items:
            func(arg); func(arg); func(arg); func(arg); func(arg)
        stop = clock()
    else:
        start = clock()
        for i in items:
            func(); func(); func(); func(); func()
        stop = clock()
    print "%15s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)) 
Example 17
Project: pyblish-win   Author: pyblish   File: test_strings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def run_test(rep, msg, func, arg):
    items = range(rep)
    from time import clock
    start = clock()
    for i in items:
        func(arg); func(arg); func(arg); func(arg); func(arg)
    stop = clock()
    print "%20s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)) 
Example 18
Project: pyblish-win   Author: pyblish   File: sortperf.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def doit(L):
    t0 = time.clock()
    L.sort()
    t1 = time.clock()
    print "%6.2f" % (t1-t0),
    flush() 
Example 19
Project: pyblish-win   Author: pyblish   File: test_time.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_clock(self):
        time.clock() 
Example 20
Project: pyblish-win   Author: pyblish   File: test_time.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_ctime_without_arg(self):
        # Not sure how to check the values, since the clock could tick
        # at any time.  Make sure these are at least accepted and
        # don't raise errors.
        time.ctime()
        time.ctime(None) 
Example 21
Project: pyblish-win   Author: pyblish   File: reperf.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def timefunc(n, func, *args, **kw):
    t0 = time.clock()
    try:
        for i in range(n):
            result = func(*args, **kw)
        return result
    finally:
        t1 = time.clock()
        if n > 1:
            print n, "times",
        print func.__name__, "%.3f" % (t1-t0), "CPU seconds" 
Example 22
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection   Author: Sunarker   File: timer.py    MIT License 5 votes vote down vote up
def tic(self, name='default'):
        # using time.time instead of time.clock because time time.clock
        # does not normalize for multithreading
        torch.cuda.synchronize()
        self._start_time[name] = time.time() 
Example 23
Project: cgp-cnn   Author: sg-nm   File: cgp.py    MIT License 5 votes vote down vote up
def _log_data(self, net_info_type='active_only'):
        log_list = [self.num_gen, self.num_eval, time.clock(), self.pop[0].eval, self.pop[0].count_active_node()]
        if net_info_type == 'active_only':
            log_list.append(self.pop[0].active_net_list())
        elif net_info_type == 'full':
            log_list += self.pop[0].gene.flatten().tolist()
        else:
            pass
        return log_list 
Example 24
Project: wikilinks   Author: trovdimi   File: builder.py    MIT License 5 votes vote down vote up
def _create_structure(self):

        # measure time
        start = time.clock()

        # creating structure
        db = MySQLDatabase(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME)
        db.build()

        seconds = round (time.clock() - start)
        logging.info('Finished after %02d:%02d minutes' % (seconds / 60, seconds % 60))
        print 'Finished after %02d:%02d minutes' % (seconds / 60, seconds % 60) 
Example 25
Project: wikilinks   Author: trovdimi   File: crawler.py    MIT License 5 votes vote down vote up
def req():
    # Get URLs from a text file, remove white space.
    db = MySQLDatabase(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME)
    db_worker_view = db.get_work_view()
    articles = db_worker_view.retrieve_all_articles()
    #articles = db_worker_view.retrieve_all_articles_questionmark()
    # measure time
    start = time.clock()
    start_time_iteration = start
    iteration_number = 483
    for i, article in enumerate(articles):
        # print some progress
        if i % 10000 == 0:
            #print time for the iteration
            seconds = time.clock() - start_time_iteration
            m, s = divmod(seconds, 60)
            h, m = divmod(m, 60)
            print "Number of crawled articles: %d. Total time for last iteration of 10000 articles: %d:%02d:%02d" % (i, h, m, s)
            start_time_iteration = time.clock()
            iteration_number += 1

        # Thread pool.
        # Blocks other threads (more than the set limit).
        pool.acquire(blocking=True)
        # Create a new thread.
        # Pass each URL (i.e. u parameter) to the worker function.
        t = threading.Thread(target=worker, args=(MEDIAWIKI_API_ENDPOINT+urllib.quote(article['title'])+'/'+str(article['rev_id']), article, iteration_number))

        # Start the newly create thread.
        t.start()
    seconds = time.clock() - start
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    print "Total time: %d:%02d:%02d" % (h, m, s) 
Example 26
Project: wikilinks   Author: trovdimi   File: mysqldatabase.py    MIT License 5 votes vote down vote up
def build(self):
        """creates up the basic database structure
        """
        logging.info('Building database structure. Opening database connection...')
        connection = self._create_connection()
        cursor = connection.cursor()
        letters = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'other')
        start = time.clock()

        # build articles table
        logging.info('Building table articles...')
        cursor.execute('CREATE TABLE `articles` ('
                           '`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,'
                           '`rev_id` BIGINT UNSIGNED NOT NULL,'
                           '`title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,'
                           'CONSTRAINT UNIQUE INDEX USING HASH(`title`),'
                           'CONSTRAINT UNIQUE INDEX USING HASH(`rev_id`)'
                       ') ENGINE=InnoDB;')

        # build redirects table
        logging.info('Building table redirects...')
        cursor.execute('CREATE TABLE `redirects` ('
                           '`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,'
                           '`source_article_name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,'
                           '`target_article_name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,'
                           'CONSTRAINT UNIQUE INDEX USING HASH(`source_article_name`)'
                       ') ENGINE = InnoDB;')



        logging.info('closing database connection...')
        connection.close()

        end = time.clock()
        logging.info('DONE building tables (took %f seconds)' % (end-start)) 
Example 27
Project: FasterRCNN_TF_Py3   Author: upojzsb   File: timer.py    MIT License 5 votes vote down vote up
def tic(self):
        # using time.time instead of time.clock because time time.clock
        # does not normalize for multithreading
        self.start_time = time.time() 
Example 28
Project: spqrel_tools   Author: LCAS   File: Kernel.py    MIT License 5 votes vote down vote up
def bootstrap(self, brainFile=None, learnFiles=[], commands=[]):
        """Prepare a Kernel object for use.

        If a brainFile argument is provided, the Kernel attempts to
        load the brain at the specified filename.

        If learnFiles is provided, the Kernel attempts to load the
        specified AIML files.

        Finally, each of the input strings in the commands list is
        passed to respond().

        """
        start = time.clock()
        if brainFile:
            self.load_brain(brainFile)

        # learnFiles might be a string, in which case it should be
        # turned into a single-element list.
        learns = learnFiles
        try:
            learns = [learnFiles + ""]
        except:
            pass
        for file in learns:
            self.learn(file)

        # ditto for commands
        cmds = commands
        try:
            cmds = [commands + ""]
        except:
            pass
        for cmd in cmds:
            print self._respond(cmd, self._globalSessionID)

        if self._verboseMode:
            print "Kernel bootstrap completed in %.2f seconds" % (time.clock() - start) 
Example 29
Project: spqrel_tools   Author: LCAS   File: Kernel.py    MIT License 5 votes vote down vote up
def load_brain(self, filename):
        """Attempt to load a previously-saved 'brain' from the
        specified filename.

        NOTE: the current contents of the 'brain' will be discarded!

        """
        if self._verboseMode: print "Loading brain from %s..." % filename,
        start = time.clock()
        self._brain.restore(filename)
        if self._verboseMode:
            end = time.clock() - start
            print "done (%d categories in %.2f seconds)" % (self._brain.numTemplates(), end) 
Example 30
Project: spqrel_tools   Author: LCAS   File: Kernel.py    MIT License 5 votes vote down vote up
def save_brain(self, filename):
        """Dump the contents of the bot's brain to a file on disk."""
        if self._verboseMode: print "Saving brain to %s..." % filename,
        start = time.clock()
        self._brain.save(filename)
        if self._verboseMode:
            print "done (%.2f seconds)" % (time.clock() - start) 
Example 31
Project: spqrel_tools   Author: LCAS   File: Kernel.py    MIT License 5 votes vote down vote up
def learn(self, filename):
        """Load and learn the contents of the specified AIML file.

        If filename includes wildcard characters, all matching files
        will be loaded and learned.

        """
        for f in glob.glob(filename):
            if self._verboseMode: print "Loading %s..." % f,
            start = time.clock()
            # Load and parse the AIML file.
            parser = AimlParser.create_parser()
            handler = parser.getContentHandler()
            handler.set_encoding(self._textEncoding)
            try:
                parser.parse(f)
            except xml.sax.SAXParseException, msg:
                err = "\nFATAL PARSE ERROR in file %s:\n%s\n" % (f, msg)
                sys.stderr.write(err)
                continue
            # store the pattern/template pairs in the PatternMgr.
            for key, tem in handler.categories.items():
                self._brain.add(key, tem)
            # Parsing was successful.
            if self._verboseMode:
                print "done (%.2f seconds)" % (time.clock() - start) 
Example 32
Project: flasky   Author: RoseOu   File: cmdline.py    MIT License 5 votes vote down vote up
def main(argv=None):
    """The main entry point to Coverage.

    This is installed as the script entry point.

    """
    if argv is None:
        argv = sys.argv[1:]
    try:
        start = time.clock()
        status = CoverageScript().command_line(argv)
        end = time.clock()
        if 0:
            print("time: %.3fs" % (end - start))
    except ExceptionDuringRun:
        # An exception was caught while running the product code.  The
        # sys.exc_info() return tuple is packed into an ExceptionDuringRun
        # exception.
        _, err, _ = sys.exc_info()
        traceback.print_exception(*err.args)
        status = ERR
    except CoverageException:
        # A controlled error inside coverage.py: print the message to the user.
        _, err, _ = sys.exc_info()
        print(err)
        status = ERR
    except SystemExit:
        # The user called `sys.exit()`.  Exit with their argument, if any.
        _, err, _ = sys.exc_info()
        if err.args:
            status = err.args[0]
        else:
            status = None
    return status 
Example 33
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: profiler_executor.py    Apache License 2.0 5 votes vote down vote up
def benchmark(mod, dry_run=10, iterations=10):
    if len(mod._context) == 1:
        ctx = mod._context[0]
    else:
        ctx = mx.cpu()
    data = [mx.random.uniform(-1.0, 1.0, shape=shape, ctx=ctx) for _, shape in mod.data_shapes]
    label = [mx.nd.array(np.random.randint(1, 100, size=shape), ctx=ctx) for _, shape in mod.label_shapes]
    batch = mx.io.DataBatch(data, label)

    # dry run
    for i in range(dry_run):
        mod.forward(batch, is_train=True)
        mod.backward()
        for output in mod.get_outputs(merge_multi_context=False)[0]:
            output.wait_to_read()
        mod.update()

    t0 = time.clock()

    profiler.set_state('run')
    # real run
    for i in range(iterations):
        mod.forward(batch, is_train=True)
        mod.backward()
        mod.update()
        for output in mod.get_outputs(merge_multi_context=False)[0]:
            output.wait_to_read()
    profiler.set_state('stop')

    t1 = time.clock()
    return (t1 - t0)*1000.0 / iterations 
Example 34
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_profiler.py    Apache License 2.0 5 votes vote down vote up
def test_profiler():
    iter_num = 5
    begin_profiling_iter = 2
    end_profiling_iter = 4

    enable_profiler('test_profiler.json', False, False)

    A = mx.sym.Variable('A')
    B = mx.sym.Variable('B')
    C = mx.symbol.dot(A, B)

    executor = C.simple_bind(mx.cpu(1), 'write', A=(4096, 4096), B=(4096, 4096))

    a = mx.random.uniform(-1.0, 1.0, shape=(4096, 4096))
    b = mx.random.uniform(-1.0, 1.0, shape=(4096, 4096))

    a.copyto(executor.arg_dict['A'])
    b.copyto(executor.arg_dict['B'])

    print("execution begin")
    for i in range(iter_num):
        print("Iteration {}/{}".format(i + 1, iter_num))
        if i == begin_profiling_iter:
            t0 = time.clock()
            profiler.set_state('run')
        if i == end_profiling_iter:
            t1 = time.clock()
            profiler.set_state('stop')
        executor.forward()
        c = executor.outputs[0]
        c.wait_to_read()
    print("execution end")
    duration = t1 - t0
    print('duration: {0}s'.format(duration))
    print('          {0}ms/operator'.format(duration*1000/iter_num))
    profiler.dump(True)
    profiler.set_state('stop') 
Example 35
Project: prep   Author: AbhiAgarwal   File: bfs.py    MIT License 5 votes vote down vote up
def visited(self, value):
        if value == 1:
            self.discovery_time = time.clock()
        elif value == 2:
            self.finishing_time = time.clock()

        self._visited = value 
Example 36
Project: prep   Author: AbhiAgarwal   File: bfs_dfs.py    MIT License 5 votes vote down vote up
def visited(self, value):
        if value == 1:
            self.discovery_time = time.clock()
        elif value == 2:
            self.finishing_time = time.clock()

        self._visited = value 
Example 37
Project: GSM-SGD   Author: DingXiaoH   File: timer.py    MIT License 5 votes vote down vote up
def tic(self):
        # using time.time instead of time.clock because time time.clock
        # does not normalize for multithreading
        self.start_time = time.time() 
Example 38
Project: BayesRate   Author: schnitzler-j   File: BayesRateTDI.py    MIT License 5 votes vote down vote up
def startMCMC(i):
	global bd, exp_M, rj, categ
	t1 = time.clock()
	MCMC(K,i, runs, iteration, burnin, categ, rep, mod_times, mod_rates, rj, sample_freq, rand_tree, bd, exp_M, model,\
	lam_rA, lam_mA, path_dir, name_file, fixed_times, prior_shift, sample_fraction, print_freq, fix_k,0,BETA)
	if i==0: print "\n MCMC execution time: {0:.2f} min".format((time.clock()-t1)/60.)
	if runs>1 and i==0: print " finishing remote chains..." 
Example 39
Project: BayesRate   Author: schnitzler-j   File: BayesRateCladeTDI.py    MIT License 5 votes vote down vote up
def startMCMC(i):
	t1 = time.clock()
	MCMC([trees_ingroup, input_file, name_file, path_dir, part_file, BETA], i, runs, 0)
	if i==0: print "\n MCMC execution time: {0:.2f} min".format((time.clock()-t1)/60.)
	if runs>1 and i==0: print " finishing remote chains..." 
Example 40
Project: BayesRate   Author: schnitzler-j   File: BayesRateCladeTDIthreads.py    MIT License 5 votes vote down vote up
def startMCMC(part_file, i, thread):
	#t1 = time.clock()
	arg=list()
	r.append(MCMC([trees_ingroup, input_file, name_file, path_dir, part_file, BETA], i, runs, 0))
	#if i==0: print "\n MCMC execution time: {0:.2f} min".format((time.clock()-t1)/60.)
	if runs>1 and i==0: print " finishing remote chains..." 
	if len(r)==runs:
		part_file, input_file_, name_file_, path_di_r=fix_path(part_file)
		f = open(part_file, 'r')
		L=f.readlines()
		for l, line in enumerate(L):
			line= line.split()
			if "output_file" in line: 
				output_file=line[1]
				break

		out_list=list()
		for i in range(0, runs): 
			out_file = "%sBFlogs/%s_sum_%s.txt" % (path_dir,output_file, i)
			out_list.append(out_file)
		print " summarizing %s runs..." % (runs)

		import mcmc_tdi
		temperatures=mcmc_tdi.temp_gen(BETA, runs)
		marginal_likelihood(runs, out_list, path_dir, name_file, output_file, temperatures)

		print "\n Use 'Ctrl-C' to quit.\n" 
Example 41
Project: RLBotPack   Author: RLBot   File: floating_setup.py    MIT License 5 votes vote down vote up
def should_call_bakkes(player_index):
    # Note: this function mutates external state: last_bakkes_call.
    global last_bakkes_call
    now = time.clock()
    if now - last_bakkes_call.get(player_index, 0) > MIN_DELAY_BETWEEN_BAKKES_CALLS:
        last_bakkes_call[player_index] = now
        return True
    return False 
Example 42
Project: RLBotPack   Author: RLBot   File: floating_setup.py    MIT License 5 votes vote down vote up
def set_random_rotation_and_angular_vel_periodically(player_index, period=2.0):
    now = time.clock()
    global last_rotation_modification
    if now - last_rotation_modification.get(player_index, 0) > period:
        last_rotation_modification[player_index] = now
        set_random_rotation_and_angular_vel(player_index) 
Example 43
Project: ngo-addons-backport   Author: camptocamp   File: models.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def consume_cpu_time(self, cr, uid, seconds, context=None):
        t0 = time.clock()
        t1 = time.clock()
        while t1 - t0 < seconds:
            for i in xrange(10000000):
                x = i * i
            t1 = time.clock()
        return True
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: 
Example 44
Project: attack-graphs   Author: cyberImperial   File: benchmarks.py    MIT License 5 votes vote down vote up
def take_snapshot():
    t = time.clock()
    try:
        graph = LocalClient(config["graph"]).get("/graph")
        graph = Graph.from_json(graph)
        return t, graph
    except Exception as e:
        return None 
Example 45
Project: EXOSIMS   Author: dsavransky   File: run_ipcluster_ensemble.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_one(genNewPlanets=True, rewindPlanets=True, outpath='.'):    
    # wrap the run_sim in a try/except loop
    nbmax = 10
    for attempt in range(nbmax):
        try:
            # run one survey simulation
            SS.run_sim()
            DRM = SS.DRM[:]
            systems = SS.SimulatedUniverse.dump_systems()
            systems['MsTrue'] = SS.TargetList.MsTrue
            systems['MsEst'] = SS.TargetList.MsEst
            seed = SS.seed
        except Exception as e:
            # if anything goes wrong, log the error and reset simulation
            with open(os.path.join(outpath,'log.err'), 'ab') as f:
                f.write(repr(e))
                f.write('\n')
                f.write(traceback.format_exc())
                f.write('\n\n')
            
            SS.reset_sim()
        else:
            break
    else:
        raise ValueError("Unsuccessful run_sim after %s reset_sim attempts"%nbmax)
    
    # reset simulation at the end of each simulation
    SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)
    
    pklname = 'run'+str(int(time.clock()*100))+''.join(["%s" % random.randint(0, 9) for num in range(5)]) + '.pkl'
    pklpath = os.path.join(outpath, pklname)
    with open(pklpath, 'wb') as f:
        pickle.dump({'DRM':DRM,'systems':systems,'seed':seed}, f)
        
    return 0 
Example 46
Project: EXOSIMS   Author: dsavransky   File: runQueue.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_one(genNewPlanets=True, rewindPlanets=True, outpath='.'):
    # wrap the run_sim in a try/except loop
    # reset simulation at the end of each simulation
    #NOTE: Methods are imported from IPClusterEnsemble sync_imports function line
    SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)
    nbmax = 10
    for attempt in numpy.arange(nbmax):
        try:
            # run one survey simulation
            SS.run_sim()
            DRM = SS.DRM[:]
            systems = SS.SimulatedUniverse.dump_systems()
            systems['MsTrue'] = SS.TargetList.MsTrue
            systems['MsEst'] = SS.TargetList.MsEst
            seed = SS.seed
        except Exception as e:
            # if anything goes wrong, log the error and reset simulation
            with open(os.path.join(outpath,'log.err'), 'ab') as f:
                f.write(repr(e))
                f.write('\n')
                f.write(traceback.format_exc())
                f.write('\n\n')
            
            SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)
        else:
            break
    else:
        raise ValueError("Unsuccessful run_sim after %s reset_sim attempts"%nbmax)
    
    # reset simulation at the end of each simulation
    SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)  

    pklname = 'run'+str(int(time.clock()*100))+''.join(["%s" % random.randint(0, 9) for num in numpy.arange(5)]) + '.pkl'
    pklpath = os.path.join(outpath, pklname)
    with open(pklpath, 'wb') as f:
        pickle.dump({'DRM':DRM,'systems':systems,'seed':seed}, f)
        
    return 0 
Example 47
Project: kaggle-carvana-2017   Author: killthekitten   File: predict_masks.py    MIT License 5 votes vote down vote up
def predict():
    output_dir = args.pred_mask_dir
    model = make_model((None, None, 3))
    model.load_weights(args.weights)
    batch_size = args.pred_batch_size
    nbr_test_samples = 100064

    filenames = [os.path.join(args.test_data_dir, f) for f in sorted(os.listdir(args.test_data_dir))]

    start_time = clock()
    for i in range(int(nbr_test_samples / batch_size) + 1):
        x = []
        for j in range(batch_size):
            if i * batch_size + j < len(filenames):
                img = load_img(filenames[i * batch_size + j], target_size=(args.img_height, args.img_width))
                x.append(img_to_array(img))
        x = np.array(x)
        x = preprocess_input(x, args.preprocessing_function)
        x = do_tta(x, args.pred_tta)
        batch_x = np.zeros((x.shape[0], 1280, 1920, 3))
        batch_x[:, :, 1:-1, :] = x
        preds = model.predict_on_batch(batch_x)
        preds = undo_tta(preds, args.pred_tta)
        for j in range(batch_size):
            filename = filenames[i * batch_size + j]
            prediction = preds[j][:, 1:-1, :]
            array_to_img(prediction * 255).save(os.path.join(output_dir, filename.split('/')[-1][:-4] + ".png"))
        time_spent = clock() - start_time
        print("predicted batch ", str(i))
        print("Time spent: {:.2f}  seconds".format(time_spent))
        print("Speed: {:.2f}  ms per image".format(time_spent / (batch_size * (i + 1)) * 1000))
        print("Elapsed: {:.2f} hours  ".format(time_spent / (batch_size * (i + 1)) / 3600 * (nbr_test_samples - (batch_size * (i + 1))))) 
Example 48
Project: CSD-SSD   Author: soo89   File: eval512.py    MIT License 5 votes vote down vote up
def tic(self):
        # using time.time instead of time.clock because time time.clock
        # does not normalize for multithreading
        self.start_time = time.time() 
Example 49
Project: CSD-SSD   Author: soo89   File: eval.py    MIT License 5 votes vote down vote up
def tic(self):
        # using time.time instead of time.clock because time time.clock
        # does not normalize for multithreading
        self.start_time = time.time() 
Example 50
Project: Repobot   Author: Desgard   File: _UserFriendlyRNG.py    MIT License 5 votes vote down vote up
def collect(self):
        # Collect 64 bits of entropy from the operating system and feed it to Fortuna.
        self._osrng_es.feed(self._osrng.read(8))

        # Add the fractional part of time.time()
        t = time.time()
        self._time_es.feed(struct.pack("@I", int(2**30 * (t - floor(t)))))

        # Add the fractional part of time.clock()
        t = time.clock()
        self._clock_es.feed(struct.pack("@I", int(2**30 * (t - floor(t)))))