loop

Insert sql query in loop a good practice or bad?

匿名 (未验证) 提交于 2019-12-03 08:39:56
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a list of users which needs to be iterated using a foreach loop and inserted in to a table for every new row in db table. $data [ 'entity_classid' ] = $classid ; $data [ 'notification_context_id' ] = $context_id ; $data [ 'entity_id' ] = $entity_id ; $data [ 'notification_by' ] = $userid ; $data [ 'actionid' ] = $actionid ; $data [ 'is_read' ] = 0 ; $data [ 'createdtime' ] = time (); foreach ( $classassocusers as $users ){ $data [ 'notification_to' ] = $users -> userid ; $DB -> insert_record ( 'homework.comments' , $data ,

Angular2 - *ngFor / loop through json object with array

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Want to loop through json object //Defined on component side : jsonObj = { '1' : [ {"title" : "title1" , "desc" : "desc1" }], '2' : [ {"title" : "title2" , "desc" : "desc2" }], '3' : [ {"title" : "title3" , "desc" : "desc3" }], '4' : [ {"title" : "title4" , "desc" : "desc4" }], '5' : [ {"title" : "title5" , "desc" : "desc5" }] } With *ngFor only on template side , And without any coding (function) on component side . Want to print just each title and desc Is this possible ? If Yes ? How ? 回答1: Till now the best / shortesr answer I found is

Event Loop的规范和实现

别来无恙 提交于 2019-12-03 07:57:27
本文转载于: 猿2048 网站➫ https://www.mk2048.com/blog/blog.php?id=hcabiha12j 作者简介:nekron 蚂蚁金服·数据体验技术团队 序 一直以来,我对 Event Loop 的认知界定都是可知可不知的分级,因此仅仅保留浅显的概念,从未真正学习过,直到看了这篇文章—— 《这一次,彻底弄懂 JavaScript 执行机制》 。该文作者写的非常友好,从最小的例子展开,让我获益匪浅,但最后的示例牵扯出了 chrome 和 Node 下的运行结果迥异,我很好奇,我觉得有必要对这一块知识进行学习。 由于上述原因,本文诞生,原本我计划全文共分3部分来展开:规范、实现、应用。但遗憾的是由于自己的认知尚浅,在如何根据 Event Loop 的特性来设想应用场景时,实在没有什么产出,导致有关应用的篇幅过小,故不在标题中作体现了。 (本文所有代码运行环境仅包含Node v8.9.4以及 Chrome v63) PART 1:规范 为什么要有Event Loop? 因为Javascript设计之初就是一门单线程语言,因此为了实现主线程的不阻塞, Event Loop 这样的方案应运而生。 小测试(1) 先来看一段代码,打印结果会是? console.log(1) setTimeout(() => { console.log(2) }, 0)

How to use 'yield' inside async function?

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to use generator yield and async functions. I read this topic , and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): mygenerator = await createGenerator() for i in mygenerator: print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(start()) except KeyboardInterrupt: loop.stop() pass But i got the error: SyntaxError: 'yield' inside async function How to use yield generator in async function? 回答1: Upd: Starting with Python 3

Libev库学习

纵然是瞬间 提交于 2019-12-03 04:32:33
Libev库学习 https://www.cnblogs.com/wunaozai/p/3950249.html Libev库学习(1) https://www.cnblogs.com/wunaozai/p/3954131.html Libev库学习(2) https://www.cnblogs.com/wunaozai/p/3955156.html Libev库学习(3) https://www.cnblogs.com/wunaozai/p/3960494.html Zlib库的安装与使用 https://segmentfault.com/a/1190000006173864 01:概述和 ev_loop https://segmentfault.com/a/1190000006200077 02:watcher 基础 https://segmentfault.com/a/1190000006679929 03:常用 watcher 接口 https://www.cnblogs.com/gqtcgq/p/7247102.html Libev中的相对时间定时器 ev_timer https://www.cnblogs.com/gqtcgq/p/7247100.html Libev中的绝对时间定时器 ev_periodic https://www.cnblogs.com/gqtcgq

GCC 5.1 Loop unrolling

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Given the following code #include <stdio.h> int main(int argc, char **argv) { int k = 0; for( k = 0; k < 20; ++k ) { printf( "%d\n", k ) ; } } Using GCC 5.1 or later with -x c -std=c99 -O3 -funroll-all-loops --param max-completely-peeled-insns=1000 --param max-completely-peel-times=10000 does partially loop unrolling, it unrolls the loop ten times and then does a conditional jump. .LC0: .string "%d\n" main: pushq %rbx xorl %ebx, %ebx .L2: movl %ebx, %esi movl $.LC0, %edi xorl %eax, %eax call printf leal 1(%rbx), %esi movl $.LC0, %edi xorl

How to create an async generator in Python?

匿名 (未验证) 提交于 2019-12-03 03:00:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to rewrite this Python2.7 code to the new async world order: def get_api_results(func, iterable): pool = multiprocessing.Pool(5) for res in pool.map(func, iterable): yield res map() blocks until all results have been computed, so I'm trying to rewrite this as an async implementation that will yield results as soon as they are ready. Like map() , return values must be returned in the same order as iterable . I tried this (I need requests because of legacy auth requirements): import requests def get(i): r = requests.get('https:/

Loop through all elements with class 'blah' and find the highest id value

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a bunch of elements like: <div id = "car-123" class = "blah" > .. </div> I want to loop through all of them and get the highest ID i.e. 123 how to do this? Is below correct and the best way? $ ( ".blah" ). each ( function () { var id = $ ( this ). attr ( 'id' ). split ( '-' )[ 0 ]; if ( id > newid ) newid = id ; }); 回答1: I would do: var max = 0 ; $ ( ".blah" ). each ( function (){ num = parseInt ( this . id . split ( "-" )[ 1 ], 10 ); if ( num > max ) { max = num ; } }); Most people would do this way. 回答2: I'd go for this,

While loop freezes game in Unity3D

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've always struggled with while loops because they barely ever work for me. They always cause my Unity3D application to freeze, but in this instance I really need it to work: bool gameOver = false; bool spawned = false; float timer = 4f; void Update () { while (!gameOver) { if (!spawned) { //Do something } else if (timer >= 2.0f) { //Do something else } else { timer += Time.deltaTime; } } } Ideally, I want those if statements to run as the game runs. Right now it crashes the program and I know it's the while loop which is the problem

Why Rcpp::warning() is slower than R warning?

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Consider the following functions foo and bar (differs only in printing warning) and their R equivalents fooR and barR : cppFunction(' void foo () { int i = 0; while (i < 1e3) { i++; } return; }') cppFunction(' void bar () { int i = 0; while (i < 1e3) { i++; Rcpp::warning("hello world!"); } return; }') fooR <- function() { i = 0; while (i < 1e3) { i = i+1; } } barR <- function() { i = 0; while (i < 1e3) { i = i+1; warning("hello world!") } } Obviously, printing warnings makes function slower, but the difference between R and Rcpp is huge (200