To quote the official deque documentation: But who cares? enqueue() remains pitifully slow. Since removing the last item from a C-based array and hence Python list is a constant-time operation, implementing the dequeue() method in terms of a Python list retains the same worst-case time complexity of O(1). As I note below, implementing the enqueue() methods in terms of a Python list increases its worst-case time complexity to O(n). It's also thread-safe and presumably more space and time efficient, given its C-based heritage. ![]() While doing so reduces the worst-case time complexity of your dequeue() and enqueue() methods to O(1), the que type already does so. What Not to DoĪvoid reinventing the wheel by hand-rolling your own: As Uri Goren astutely noted above, the Python stdlib already implemented an efficient queue on your fortunate behalf: que. log ( res26 ) // NoneĬonst res27 = await client. log ( res25 ) // 'bike:2'Ĭonst res26 = await client. log ( res24 ) // 'bike:1'Ĭonst res25 = await client. log ( res23 ) // 'bike:3'Ĭonst res24 = await client. log ( res22 ) // 3Ĭonst res23 = await client. log ( res21 ) // Ĭonst res22 = await client. lRange ( 'bikes:repairs', 0, - 1 ) console. log ( res20 ) // 5Ĭonst res21 = await client. log ( res19 ) // 3Ĭonst res20 = await client. log ( res18 ) // Ĭonst res19 = await client. log ( res17 ) // 3Ĭonst res18 = await client. lPush ( 'bikes:repairs', 'bike:important_bike' ) console. log ( res16 ) // 2Ĭonst res17 = await client. rPush ( 'bikes:repairs', 'bike:2' ) console. log ( res15 ) // 1Ĭonst res16 = await client. rPush ( 'bikes:repairs', 'bike:1' ) console. log ( res14 ) // Ĭonst res15 = await client. lRange ( 'bikes:finished', 0, - 1 ) console. log ( res13 ) // Ĭonst res14 = await client. log ( res12 ) // 'bike:2'Ĭonst res13 = await client. lMove ( 'bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT' ) console. log ( res11 ) // 2Ĭonst res12 = await client. ![]() lPush ( 'bikes:repairs', 'bike:2' ) console. log ( res10 ) // 1Ĭonst res11 = await client. lPush ( 'bikes:repairs', 'bike:1' ) console. log ( res9 ) // 0Ĭonst res10 = await client. log ( res8 ) // bike:1Ĭonst res9 = await client. log ( res7 ) // bike:2Ĭonst res8 = await client. log ( res6 ) // 2Ĭonst res7 = await client. log ( res5 ) // 1Ĭonst res6 = await client. log ( res4 ) // bike:2Ĭonst res5 = await client. log ( res3 ) // bike:1Ĭonst res4 = await client. log ( res2 ) // 2Ĭonst res3 = await client. log ( res1 ) // 1Ĭonst res2 = await client. ![]() Import assert from 'assert' import from 'redis' const client = await createClient () await client. Treat a list like a queue (first in, first out): See the complete series of list commands. If the source list is empty, the command will block until a new element becomes available.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |