def FSP_twostate(parameters, N): """Steady state distribution for a two-state model evaluated using the FSP. Arguments: parameters -- List of the four rate parameters: v12,v21,k1,k2 N -- Maximal mRNA copy number. The distribution is evaluated for n=0:N-1""" t0 = pc() v12,v21,k1,k2 = parameters A = np.array([[-v12, v21], [v12, -v21]]) T = np.diag([k1,k2]); D = np.eye(2) AG = sps.lil_matrix((2*N, 2*N), dtype=np.float64) for i in range(1,N+1): if i<N: AG[(i-1)*2:i*2,(i-1)*2:i*2] = AG[(i-1)*2:i*2,(i-1)*2:i*2]+A-T-(i-1)*D AG[i*2:(i+1)*2,(i-1)*2:i*2] = T else: AG[(i-1)*2:i*2,(i-1)*2:i*2] = AG[(i-1)*2:i*2,(i-1)*2:i*2]+A-(i-1)*D if i-1>0: AG[(i-2)*2:(i-1)*2,(i-1)*2:i*2] = (i-1)*D matrix_time = pc()-t0 t1 = pc() P = null_space(AG.toarray()) null_time = pc()-t1 t2 = pc() P = np.squeeze(P) P = P/P.sum() L = 2*N+1 P = P[0:L:2] + P[1:L:2] return P #, matrix_time, null_time
def get_details_all(path): t0 = pc() details_dir = path + "/data_json/details/" os.chdir(details_dir + 'details/') details = ['author', 'publisher', 'subjects', 'itemtype', 'itemcollection', 'itemlocation'] detail_datas = {'author': [], 'publisher': [], 'subjects': [], 'itemtype': [], 'itemcollection': [], 'itemlocation': []} for file in glob.glob("*.json"): with open(file, 'r', encoding='utf8') as f: temp = json.load(f) for detail in details: try: detail_datas[detail] = sorted(list( set(detail_datas[detail]).union(set(temp[detail])))) except: print('KeyError: %s' % detail) # 进一步切割subjects sub = set() for subject in detail_datas['subjects']: sub |= set(subject.split(', ')) detail_datas['subjects'] = sorted(list(sub)) target_file = details_dir + 'details_all.json' with open(target_file, 'w', encoding='utf') as fe: json.dump(detail_datas, fe, ensure_ascii=False) print('Cost {} s'.format(pc()-t0))
def run(): """ Run standard NMF on rank """ inputF = args.inputF xgi = args.xgi ygi = args.ygi outPrefix = args.outPrefix rank = args.rank n = args.n_run prob = args.prob ct = args.ct start_time = pc() V = read_npz(inputF) xgi = read_xgi(xgi) ygi = read_xgi(ygi) out = filter_V(V, xgi, ygi, prob, ct) V = out['V'] selt_xgi = out['xgi'] np.savetxt('.'.join([outPrefix, "xgi"]), selt_xgi, fmt="%s", delimiter="\t") selt_ygi = out['ygi'] np.savetxt('.'.join([outPrefix, "ygi"]), selt_ygi, fmt="%s", delimiter="\t") save_npz_gi(V, selt_xgi, selt_ygi, outPrefix) V = compute_tfidf(V) run_nmf(V, rank, n, outPrefix) end_time = pc() print('Used (secs): ', end_time - start_time)
def main(): start_time = pc() nice_strings = 0 f = open('../input.txt', 'r') strings = f.read().splitlines() for string in strings: if NiceString(string).is_nice(): nice_strings += 1 stop_time = pc()-start_time print ("There are {} nice strings" . format (nice_strings)) print ("-----") print ("Time: {}" . format (stop_time - start_time)) print ("*****") start_time = pc() nice_strings = 0 for string in strings: if NiceString(string).is_nice_new(): nice_strings += 1 stop_time = pc()-start_time print ("Now there are {} nice strings" . format (nice_strings)) print ("Time: {}" . format (stop_time - start_time)) print ("*****")
def start(self): # # ==================================== self.before_sleep() # ==================================== # awake = 1 # I am awake if this is zero t0 = pc() while (pc()-t0 <= self.maxsec) : # # ================================= self.awake_condition() # ================================= # exists = path.exists(self.client_lockfile) if (not exists) : awake = 0 break else : sleep(self.sleepsec) # # ==================================== self.after_sleep() # ==================================== return awake
def corpusGibbsSamplePass(amount_of_topics, document_list, document_topic_count, document_topic_sum, document_word_to_topic_matrix, topic_term_count, topic_term_sum, alpha_sum): """ Does one entire pass over the entire corpus and uses the gibbs sampling process on every word of a document :param amount_of_topics: variable K :param document_list: the entire list of documents, with a documetn being a list of terms/words :param document_list: The entire list of documents, each document is a list of terms :param document_topic_count: the count of how many words in a document have a certain documet = M*K matrix (M=amount of doc, K=amount of topics) :param document_topic_sum: The amount of words in topics a document has :param document_word_to_topic_matrix: A list of dictionaries, denoting which term has which topic per document :param topic_term_count: The amount of times a term is in a topic, an K*V matrix (k=amount of topics, V= amount of terms) :param topic_term_sum: The total amount of terms per topic (K size vector) :return: The amount of words whos topic got switched, is used to determine whether the gibbs sampling is converging """ switched = 0 t0 = pc() for doc_idx, document in enumerate(document_list): switched = gibbsSampleForWordOfDocument(amount_of_topics, doc_idx, document, document_topic_count, document_topic_sum, document_word_to_topic_matrix, switched, topic_term_count, topic_term_sum, alpha_sum) if doc_idx % 10000 == 0: print("sampled %s %% of the documents" % (str( (doc_idx / len(document_list)) * 100))) print(str(pc() - t0) + "s") return switched
def find_solution(slots, aks, iterations, fertility, mutations): """ runs the evolutionary algorithm and returns a hopefully good solution """ t0 = pc() genome = inception(slots, aks) score, messages = fitness(genome) print("Inception: created first generation genome. Fitness: {0}".format(score)) print("Starting evolution for {0} iterations with {1} mutations per generation...".format(iterations, fertility)) for generation in range(iterations): # if perfect fitness has been reached, stop computing if score == 1: print(" Generation: {0} Reached fitness of 1 which can not be increased.".format(generation)) break # generate mutated children genomes candidates = [mutate(genome, mutations) for i in range(fertility)] # find best mutation for candidate in candidates: if fitness(candidate)[0] > score: print("reassign") # use fittest for next generation genome = candidate score, messages = fitness(genome) print(" Generation: {0} Fitness: {1}".format(generation, score)), print("Done in {0} seconds. Found solution with fitness {1}".format(round(pc()-t0, 4), round(score, 6))) return (genome, score, messages)
def execute(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(int(self.config.timeout)) try: # start the stopwatch t0 = pc() connect_result = sock.connect_ex( (self.config.hostname, int(self.config.port))) if connect_result == 0: available = '1' else: available = '0' # stop the stopwatch t1 = pc() result = { 'TimeTaken': int((t1 - t0) * 1000), 'Available': available } print(f"Socket connect result: {connect_result}") # return structure with data return result except Exception as e: print( f"Failed to connect to {self.config.hostname}:{self.config.port}\n{e}" ) return {'Available': 0, 'Reason': str(e)}
def search(origin, dest, date): date = arrow.get(date) results = [[] for y in range(MAX_SEARCH)] airlines = APIHelper.getAirlines() # TODO: Implement lazy loader # Load data for each tab clicked instead of loading everything # We'll see how it goescl t0 = pc() for dateIndex in range(0, MAX_SEARCH): for airline in airlines: print(API_SEARCH.format(airline['code'], date.replace(days=(dateIndex-DATE_OFFSET)).format(DATE_FORMAT), origin, dest)) flights = requests.get(API_SEARCH.format(airline['code'], date.replace(days=(dateIndex-DATE_OFFSET)).format(DATE_FORMAT), origin, dest)) if flights.status_code != SUCCESS: break for data in flights.json(): results[dateIndex].append(Flight(data)) results[dateIndex].sort(key=lambda x: x.price) print(pc() - t0) return results
def traditional_copy(): t0 = pc() with open('read.txt') as f: with open('write.txt', 'w+') as f1: for line in f: f1.write(line) return (pc() - t0)
def manage_assignment(name, resource_url, qst_entities): """ Firstly, Download the excel resource and decompress to dict.""" from xls_data_process import excel_dict xls_dict = excel_dict(resource_url) # 获取excel文件hash title存储到dict中 if not xls_dict: return '《%s》课程excel文件获取失败!' " each coroutine execute 10 requests" MAX_WORKERS = max(2, len(qst_entities)//30 + 1) from concurrent import futures from time import perf_counter as pc t0 = pc() with futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: to_do_list = [] for qst in qst_entities: future = executor.submit(trackle_qst, xls_dict, qst) to_do_list.append(future) from tqdm import tqdm done_iter = tqdm(futures.as_completed(to_do_list), total=len(to_do_list), ncols=90) result = [] for future in done_iter: res = future.result() if res: result.append(res) msg = '《{}》已完成,用时{:.2f}s;'.format(name, pc()-t0) print(msg, '\n未保存的题数有:', len(result)) return msg
def main(): x = [n for n in range(30, 46)] y_py, y_c = [], [] for n in x: start = pc() fib_py(n) end = pc() y_py.append(end - start) for n in x: start = pc() f = Heltal(n) f.fib() end = pc() y_c.append(end - start) print(f"times py:{y_py}") ##################### print(f"times c: {y_c}") ##################### plt.plot(x, y_py, color="blue") plt.plot(x, y_c, color="red") plt.xlabel("n") plt.ylabel("processing speed") plt.title("Fibonacci speed for Python and C++") plt.show() plt.savefig("fib_speed.png") f = Heltal(47) print(f"47th fibonacci number: {f.fib()}")
def run(): """ Run standard NMF on rank """ start_time = pc() """ init input files """ normHf = args.normH statHf = args.statH outPrefix = args.outPrefix perplexity = args.perplexity inF = read_files(normHf, statHf) normH = inF['normH'] o_stat_H = inF['statH'] rank = inF['rank'] print("draw silhouette & tsne plot") o_silhouette = cal_silhouette(normH,o_stat_H) X_dist = cal_pairwise_pearson(normH) """ X_transformed = cal_tSNE(X_dist, perplexity) np.savetxt('.'.join([outPrefix, "tsne.xy"]), X_transformed, fmt= "%g", delimiter="\t") """ X_transformed = cal_umap(X_dist) np.savetxt('.'.join([outPrefix, "umap.xy"]), X_transformed, fmt= "%g", delimiter="\t") plot_silhouette_tsne(o_silhouette, X_transformed, o_stat_H, rank, outPrefix) end_time = pc() print('Used (secs): ', end_time - start_time)
def div_cost(float_i): t0 = pc() for float_s in float_i: float_s /= 3 res = pc() - t0 return res
def timing_test_ascii(): IP = "192.168.100.254" PORT = 24000 NUM_MEASUREMENTS = 100 # Default TCP socket and default setup start_time = pc() sock = SocketConnection(IP, PORT) tt.tester_default_setup(sock) sock.send("FORM:READ:DATA ASC") # ASCII mode download print("Setup took {:.2f} s".format(pc() - start_time)) # Ensure the tester is ready. sock.wait_op_complete() results = np.empty(NUM_MEASUREMENTS) for i in range(NUM_MEASUREMENTS): # Trigger! sock.send("VSA1; INIT") # Ensure the trigger is done before downloading measurement, but don't block sock.send("*WAI") start_time = pc() power_samples_bytes = sock.send_recv_all( 'CAPT:SEGM1:SIGN1:DATA:SUBS:PVT? 16000000, 0, 5e-3') results[i] = pc() - start_time sock.close() print("Sample download time; mean: {:.3f} s, variance: {:.3f} s".format( results.mean(), results.var()))
def execute(self): url = urlparse(self.endpoint) location = url.netloc if url.scheme == 'http': request = http.client.HTTPConnection(location, timeout=int(self.timeout)) if url.scheme == 'https': request = http.client.HTTPSConnection( location, timeout=int(self.timeout), context=ssl._create_unverified_context()) if 'HTTP_DEBUG' in os.environ and os.environ['HTTP_DEBUG'] == '1': request.set_debuglevel(1) path = url.path if path == '': path = '/' if url.query is not None: path = path + "?" + url.query try: t0 = pc() # perform request request.request(self.method, path, self.payload, self.headers) # read response response_data = request.getresponse() # stop the stopwatch t1 = pc() response_body = str(response_data.read().decode()) result = { 'Reason': response_data.reason, 'ResponseBody': response_body, 'StatusCode': response_data.status, 'TimeTaken': int((t1 - t0) * 1000), 'Available': '1' } if self.bodyregexmatch is not None: regex = re.compile(self.bodyregexmatch) value = 1 if regex.search(response_body) else 0 result['ResponseBodyRegexMatch'] = value if self.statuscodematch is not None: result['StatusCodeMatch'] = int( int(response_data.status) == int(self.statuscodematch)) if not result[ 'StatusCodeMatch'] and self.fail_on_statuscode_mismatch: result['Available'] = '0' # return structure with data return result except Exception as e: print(f"Failed to connect to {self.endpoint}\n{e}") return {'Available': 0, 'Reason': str(e)}
def permutate(word_list): start = pc() from itertools import permutations perms = permutations(word_list) perm_list = ["".join(perm) for perm in perms] duration = pc() - start # print(f'{len(perm_list)} permutations generated in {duration:0.04f}s') return perm_list
def benchmark(label, f, graph, start, goal, n=100): st = pc() for _ in range(n): solution = f(graph, start, goal) et = pc() print(f"{label:20}: {(et-st)/(1e3 * n):.3f} µs") print("\tsolution: ", solution, "\n")
def __call__(self, *args, **kwargs): start_time = pc() # print(f'Вызывается функция: {self.fn.__name__}') result_evaluate_function = self.fn(args, kwargs) finish_time = pc() result_evaluate_timer = finish_time - start_time print(f'Функция работала = {result_evaluate_timer}') return result_evaluate_function
def basic_transfer_test(): IP = "192.168.100.254" PORT = 24000 IQVSG_FILE = "waveforms/testfile.iqvsg" REMOTE_IQVSG_FILE = "/user/uploaded_waveform.iqvsg" # Default TCP socket and default setup start_time = pc() sock = SocketConnection(IP, PORT) tt.tester_default_setup(sock) # Get response in binary format sock.send("FORM:READ:DATA PACK") # Prepare.... sock.wait_op_complete() print("Setup took {:.2f} s".format(pc() - start_time)) ### # Upload the example waveform file. # Read file to upload with open(IQVSG_FILE, 'rb') as f: file_data = f.read() # Construct and send upload command dlen = len(file_data) dlen_len = len(str(dlen)) upload_data = b"MMEM:DATA '" + REMOTE_IQVSG_FILE.encode() + b"', " + \ b"#" + str(dlen_len).encode() + str(dlen).encode() + \ file_data sock.send(upload_data) # Load and execute the new waveform sock.send("*WAI; VSG1;" + "WAVE:LOAD '" + REMOTE_IQVSG_FILE + "';" + "WAVE:EXEC ON; WAI*") ### # Download the measured samples back # Trigger! sock.send("VSA1; INIT") # Download samples sock.send('*WAI; CAPT:SEGM1:SIGN1:DATA:IQ?') IQ_samples_bytes = sock.recv_arbitrary() # Done with the socket connection sock.close() # Convert ot float and compare IQ_samples_bin = np.frombuffer(IQ_samples_bytes, dtype=np.float32) I_samples_bin = IQ_samples_bin[::2] Q_samples_bin = IQ_samples_bin[1::2]
def run(self): logger.info('Running scheduled command "%s"' % (self)) self.update_next_execution() self.save() start = pc() call_command(self.name, *self.get_args()) logger.info('Completed scheduled command "%s" in %ss' % (self, pc() - start)) if self.delete_after_next: self.delete()
def get_publishers_json(publisher_txt, file): t0 = pc() publishers = set() lines = publisher_txt.readlines() for line in lines: publishers.add(line.split('",')[0]) publishers = sorted(list(publishers)) the_dict = {"publishers": publishers, 'num': len(publishers)} json.dump(the_dict, file, ensure_ascii=False) print("Add {} publisher to publishers.json / cost: {} s".format(len(publishers), pc() - t0))
def run(): """ Run standard NMF on rank """ start_time = pc() """ init input files """ bamf = args.bam statHf = args.statH outPrefix = args.outPrefix print("filter out bam files") generate_bams(bamf, statHf, outPrefix) end_time = pc() print('Used (secs): ', end_time - start_time)
def do_something(self, func=None): ''' do ''' start = pc() if func is None: self.sum = self.do_work() else: self.sum = func() self.duration = pc() - start self.print_out()
def decompose(self): scaler = MaxAbsScaler() scaled_feature_matrix = scaler.fit_transform(self.database_matrix) print("Starting decompose") start_time = pc() self.w = self.nmf.fit_transform(scaled_feature_matrix) end_time = pc() print("Time elapsed", end_time - start_time) self.h = self.nmf.components_ self.reduced_database_matrix = self.w self.get_decomposed_data_matrix()
def timed(fun, *args): test_num = [] for i in range(1000): t0 = pc() r = fun(*args) time_taken = (pc() - t0) * 1000 test_num.append(time_taken) avarage_time_taken = sum(test_num)/len(test_num) print('{} execution took {} miliseconds.'.format(fun.__name__, avarage_time_taken)) return avarage_time_taken
def compare_fib(n): start = pc() fib_py(n) end = pc() py_time = end - start start = pc() h = Heltal(n) h.fib() end = pc() c_time = end - start return py_time, c_time
def operate(self, number, sid): ''' Подсчет результата и запись его в БД. Для запуска в отдельном потоке :param number: введенное число :param sid: id сессии ''' timer = pc() answer = json.dumps(list(factorize(number))) timer = round(pc() - timer, 3) conn = sql.connect("sessions.db") conn.execute("INSERT INTO sessions VALUES(?, ?, ?, ?, ?)", (sid, str(number), answer, timer, datetime.now())) conn.commit()
def test1(): floats = numpy.loadtxt('floats-10M-lines.txt') print(floats[-3:]) floats *= .5 print(floats[-3:]) from time import perf_counter as pc t0 = pc() floats /= 3 print(pc() - t0) numpy.save('floats-10M', floats) floats2 = numpy.load('floats-10M.npy', 'r+') floats2 *= 6 print(floats2[-3:])
def timer(title: str) -> None: """ Measures time elapsed in current block :param title: Name of block to be visible in output :return: """ from time import perf_counter as pc start = pc() try: yield finally: timediff = (pc() - start) * 1000 log.debug("It took {0} ms to execute block '{1}'".format(timediff, title))
def test_rc6(data, opmode): cr = Crypto("RC6", opmode) time = pc() encrypted = cr.encrypt(data) time = int((pc() - time) * 1000) print("Encrypted data (%d ms):" % time, encrypted) # ((len(encrypted) * "{:02X}").format(*list(encrypted)))) time = pc() decrypted = cr.decrypt(encrypted) time = int((pc() - time) * 1000) print("Decrypted data (%d ms):" % time, decrypted) if decrypted == data: print("test passed") else: print("test failed! %s != %s" % (decrypted, data))
def clocked(*args, **kwargs): t0 = pc() result = func(*args) elapsed_time = pc() - t0 name = func.__name__ arg_lst = [] if args: arg_lst.append(', '.join(repr(arg) for arg in args)) if kwargs: pairs = ['%s=%r' % (k, w) for k, w in sorted(kwargs.items())] arg_lst.append(', '.join(pairs)) arg_str = ', '.join(arg_lst) print('[%0.8fs] %s(%s) -> %r' % (elapsed_time, name, arg_str, result)) return result
def main(): input_lists = args.input outf = args.output start_time = pc() npz_list = [] for i in input_lists: print(i) npz = load_npz(i) npz_list.append(npz) del (npz) merged_npz = vstack(npz_list) outnpz = ".".join([outf, "npz"]) save_npz(outnpz, merged_npz) end_time = pc() print('Used (secs): ', end_time - start_time)
def write_test_1(block_size, blocks_count): file = 'write_speed_1.file' f = os.open(file, os.O_CREAT | os.O_WRONLY, 0o777) # low-level I/O took = [] for i in range(blocks_count): buff = os.urandom(block_size) # get random bytes start = pc() os.write(f, buff) os.fsync(f) # force write to disk t = pc() - start took.append(t) os.close(f) return took
def timed(name): start = pc() def with_function(result): end = pc() print(name + ": " + str(end - start) + "s") return result return with_function
def execCmdSearchExe(hostname,userName,mac,drive,guid,isAuto): #print(path+"---"+fnexp) cols =[] start=pc() print(start) for filename,filePath,root in searchEXE(drive): file=Common.file.File().GetCompanyNameAndProductName(filePath) if file: col = {"Computer":hostname,"Mac":mac,"UserName":userName,"FileName":filename,"FilePath":filePath,"GUID":guid,"IsAuto":isAuto ,"CompanyName":file["CompanyName"],"ProductName":file["ProductName"] ,"LegalCopyright":file["LegalCopyright"] ,"Address":Address } InseretToSqlServer(hostname,userName,mac,filename,filePath,guid,isAuto,file["CompanyName"],file["ProductName"],file["LegalCopyright"]) else: col = {"Computer":hostname,"Mac":mac,"UserName":userName,"FileName":filename,"FilePath":filePath,"GUID":guid,"IsAuto":isAuto ,"CompanyName":"","ProductName":"" ,"LegalCopyright":"" ,"Address":Address } InseretToSqlServer(hostname,userName,mac,filename,filePath,guid,isAuto,"","","") cols.append(col) endSql=pc() print(endSql) print(endSql - start) db = DBUtility.mongodb.MongoDB().get_db() DBUtility.mongodb.MongoDB().insert_mulit_docs(db,"tdiFilesEXE",cols) end=pc() print("%s" % end) print(end - endSql)
''' By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? ''' # finding primes is an ancient task # lets try the naieve approach and see how long it takes to get the first 1000 primes import math as m from time import sleep, perf_counter as pc timer = pc() def primer(nth_prime): primes = [2] x = 3 while (len(primes) < nth_prime): c = True for y in range(3,m.floor(x/2)+1,2): # if x%y==0: c = False if (c==True): primes.append(x) x+=2 return primes[-1] print(str(primer(10001))) print(pc()-timer) # 104743 # 52 seconds, not amazing but this isnt going in production # or I would probably drop to numpy/stackoverflow
def testWL(algo, g1, g2): t0 = pc() isomorphic = algo.execute(g1, g2, 15) print("WL: " + str(isomorphic)) return pc() - t0
print("a[2]: {}".format(a[2])) print("a[:, 1]: {}".format(a[:, 1])) # a.transpose() # NB: for some reason transpose isn't destructive? lovely, lovely consistency :/ print("a.transpose(): {}".format(a.transpose())) def make_floats_file(fname, nitems): with open(fname, 'w') as fh: for idx in range(nitems): print("{}".format(str(random.random())), file=fh) return True if not os.path.isfile('floats-10M-lines.txt'): # make_floats_file('floats-10M-lines.txt', 10000000) make_floats_file('floats-10M-lines.txt', 1000) floats = numpy.loadtxt('floats-10M-lines.txt') print("floats[-3:]: {}".format(floats[-3:])) floats *= 0.5 print("floats[-3:]: {}".format(floats[-3:])) stime = pc() floats /= 3 etime = pc() print("stime: {}; etime: {}; elapsed: {}".format(stime, etime, etime-stime)) numpy.save('floats.bin', floats) # WOW, super awesome that it automatically appends a .npy to the file name floats2 = numpy.load('floats.bin.npy', 'r+') floats2 *= 6 print("floats2[-3:]: {}".format(floats2[-3:]))
a.shape = 3, 4 print(a) print(a[2]) print(a[2, 1]) print(a[:, 1]) print(a.transpose()) try: floats = numpy.loadtxt('floats-10M-lines.txt') except OSError: print("File not found") else: print(floats[-3:]) floats *= .5 print(floats[-3:]) t0 = pc(); floats /= 3; print(pc() - t0) numpy.save('floats-10M', floats) floats2 = numpy.load('floats-10M.npy', 'r+') floats2 *= 6 print(floats[-3:]) """ Deques and queues => Fast inserting and removing from both ends of the list Other queues: queue; multiprocessing; asyncio; heapq. """ print("\nDeques and queues") dq = deque(range(10), maxlen=10) print(dq) dq.rotate(-4) print(dq) dq.appendleft(-1)
def main(): graph_st = [] graph = [] run_multiple = 0 theMinimumCut = 0 loops = 0 if len( sys.argv ) < 2: usage() return if re.match( '-h', sys.argv[ 1 ] ): usage() return for switch in sys.argv: if re.match( '-nsquared', switch ): run_multiple = 1 sys.argv.remove( '-nsquared' ) break for switch in sys.argv: m = re.match( '-(\d+)', switch ) if m: loops = int( m.group(1) ) sw = '-' + str( loops ) sys.argv.remove( sw ) break inputFileName = sys.argv[ 1 ] # read file into an array directly with open( inputFileName, 'rt' ) as fin: while True: line = fin.readline() if not line: break graph_st.append( line ) # convert string graph into integer graph; # first element is node; other elements are adjacent nodes for line_st in graph_st: adjacents = [ int( i ) for i in line_st.split() ] graph.append( adjacents ) # starting graph pprint.pprint( graph, width=200 ) # call min-cut algorithm if loops: trials = loops elif run_multiple: trials = len( graph ) * len( graph ) else: trials = 1 # time it t0 = pc() for i in range( trials ): trial_graph = deepcopy( graph ) min_cut( trial_graph ) currentMin = len( trial_graph[ 0 ] ) - 1 if i == 0 or currentMin < theMinimumCut: theMinimumCut = currentMin t1 = pc() # the number of edges between the two remaining end point is the min cut if trials == 1: print( 'graph = {}; minimum cut = {:d}'.format( graph, theMinimumCut ) ) else: print( 'minimum cut = {:d}; elapsed time = {:f}'.format( theMinimumCut, t1 - t0 ) ) return
def testVF2(g1, g2): t0 = pc() print("VF2: " + str(g1.isomorphic(g2))) return pc() - t0
Created on 3 июня 2016 г. @author: Михаил Булыгин <*****@*****.**> ''' from elasticsearch import Elasticsearch from pg import DB from pgdb import connect from time import perf_counter as pc if __name__ == '__main__': es = Elasticsearch() con = connect(user="******", password="******", database="wiki") cur = con.cursor() cur.execute("SELECT COUNT(*) FROM wiki") count = cur.fetchone().count cur.execute("SELECT * FROM wiki") t = pc() for i in range(count): row = cur.fetchone() doc = {"title": row.title, "timestamp": row.timestamp, "author": row.author, "text": row.text} es.index(index="wiki", doc_type="article", body=doc, id=row.id) if i % 100 == 0: elapsed = pc() - t remaining = elapsed / (i+1) * count print("Elapsed: {:.0f}m {:.0f}s, Remaining: {:.0f}m {:.0f}s, Article {} of {}: {} ({:.2%})". format(elapsed/60, elapsed%60, remaining/60, remaining%60, i, count, row.title, i/count))
# Uses python3 from time import sleep, perf_counter as pc t0 = pc() def calc_fib(n): if (n <= 1): return n return calc_fib(n - 1) + calc_fib(n - 2) dic = {'key': 0} def calc_fib_more_faster(n): if (n <= 1): return n if (n - 1) in dic: a = dic[n - 1] else: dic[n - 1] = calc_fib_more_faster(n - 1) a = dic[n - 1] if (n - 2) in dic: b = dic[n - 2] else: dic[n - 2] = calc_fib_more_faster(n - 2)
def with_function(result): end = pc() print(name + ": " + str(end - start) + "s") return result