好吧,对于我的编程任务(是的,我们都可以转向我们认为适合帮助的任何源)我必须找出进程花费阻塞/休眠/运行的时间.
我的第一次尝试是创建一个bash脚本......看起来像这样:
for i in `ls /proc/ | egrep [0-9]+`
do
cat /proc/$i/status | grep State
done
Run Code Online (Sandbox Code Playgroud)
但后来所有问题都报告了睡眠状态.加上这种方法需要我疯狂地进行调查......所以运行测试可能会改变结果......(呃)
现在用syscalls编译新版本的linux或者跟踪进程状态的方法并不是不可能的.我唯一担心的是试图找出如何跟踪不断变化的过程状态,并确保我不会错过任何东西......
我有一个学校项目,我必须在其中实现一个聊天应用程序,其服务器将是一个java Web服务.
问题是我一直认为Web服务是一种调用远程功能的方式,我不知道如何在Web服务上保持"会话"活动,也不知道如何跟踪当前所有人聊天,房间等
我被分配了读取.txt文件的任务,该文件是各种事件的日志,并将一些事件写入字典.
问题是该文件有时可能大于3GB.这意味着字典太大而无法放入主内存中.Shelve似乎是解决这个问题的好方法.但是,由于我将不断修改字典,我必须writeback启用该选项.这是我担心的地方 - 教程说这会减慢读/写过程并使用更多内存,但我无法找到有关速度和内存如何受影响的统计信息.
任何人都可以澄清读/写速度和内存受到多大影响,以便我可以决定是使用回写选项还是牺牲一些可读性来提高代码效率?
谢谢
我有一个非常大的制表符分隔的文本文件.文件中的许多行对于文件中的一列具有相同的值.我想把它们放在同一行.例如:
a foo
a bar
a foo2
b bar
c bar2
Run Code Online (Sandbox Code Playgroud)
运行脚本后,它应该成为:
a foo;bar;foo2
b bar
c bar2
Run Code Online (Sandbox Code Playgroud)
我怎样才能在shell脚本或Python中执行此操作?
谢谢.
def filter(f, lst):
if lst == []: return []
if f(lst[0]): return [lst[0]] + filter(f, lst[1:])
return filter(f, lst[1:])
def my_reverse(lst): # Reverse the list
def reverse_helper(x,y):
if x == []: return y
return reverse_helper(x[1:], [x[0]] + y)
return reverse_helper(lst, [])
def revfilter_alpha(f, lst): # Reverse and filter ...
return my_reverse(filter(f, lst))
def revfilter_beta(f, lst): # Reverse and filter ...
if lst == []: return []
return revfilter_beta(f, lst[1:]) + ([lst[0]] if f(lst[0]) else [])
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释如何确定BigΘ符号的运行时间吗?我已经阅读了很多东西,但仍然不知道从哪里开始.
在filter,我认为它是Θ(n ^ …
我有一个类(让我们称之为myClass)实现两者__hash__和__eq__.我也有一个dict将myClass对象映射到某个值,计算需要一些时间.
在我的程序过程中,许多(大约数百万)myClass对象被实例化.这就是我使用它dict来跟踪这些值的原因.
但是,有时新myClass对象可能等同于旧对象(由__eq__方法定义).因此,我不是再次计算该对象的值,而是仅查找旧myClass对象的值dict.要做到这一点,我做到了if myNewMyClassObj in dict.
这是我的问题:
当我使用该in子句时,会调用什么,__hash__或者__eq__?使用a的关键dict是它是O(1)查找时间.所以__hash__必须要打电话.但是,如果__hash__和__eq__不是等价的方法呢?在那种情况下,我会得到误报if myNewMyClassObj in dict吗?
跟进问题:
我想最小化我的条目数dict,所以我最好只保留一组等效myClass对象中的一个dict.再次,似乎__eq__需要在计算时调用if myNewClassObj in dict,这会将dictO(1)查找时间玷污为O(n)查找时间
我使用下划线来表示未知单词的长度.如何在没有代表列表的括号的情况下仅打印下划线?
基本上,如果我有一个表单列表['_', '_', '_', '_'],我想打印下划线而不用列表语法打印它们"_ _ _ _"
我正在尝试编写一个可以调整所有打开窗口大小的applescript脚本.为了确保我到达所有窗口,我正在让我的脚本说出应用程序的名称以及该应用程序的打开窗口数.
有趣的是,当我听到所有打开的应用程序的名称时,我的脚本说他们都打开了0个窗口.我该如何解决这个问题?
这是我的代码:
tell application "System Events"
repeat with theProcess in (every process)
if background only of theProcess is false then
if name of theProcess is not "Finder" then
if name of theProcess is "Google Chrome" then
say "Chrome woo hoo"
say (count windows as string)
else
say name of theProcess as string
say (count windows as string)
tell theProcess
repeat with theWindow in windows
say "found a window of"
say (name of theProcess) as string
tell theWindow
click button …Run Code Online (Sandbox Code Playgroud) 我正在使用concurrent.futures.ThreadPoolExecutor来查看我是否可以从我的四核处理器(具有8个逻辑核心)中挤出更多工作.所以我写了下面的代码:
from concurrent import futures
def square(n):
return n**2
def threadWorker(t):
n, d = t
if n not in d:
d[n] = square(n)
def master(n, numthreads):
d = {}
with futures.ThreadPoolExecutor(max_workers=numthreads) as e:
for i in e.map(threadWorker, ((i, d) for i in range(n))):
pass # done so that it actually fetches each result. threadWorker has its own side-effects on d
return len(d)
if __name__ == "__main__":
print('starting')
print(master(10**6, 6))
print('done')
Run Code Online (Sandbox Code Playgroud)
有趣的是,在for循环中编写相同的功能需要大约一秒钟:
>>> d = {}
>>> for i in range(10**6): …Run Code Online (Sandbox Code Playgroud) python multithreading python-3.x threadpoolexecutor concurrent.futures
这是观察到的行为:
In [4]: x = itertools.groupby(range(10), lambda x: True)
In [5]: y = next(x)
In [6]: next(x)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-6-5e4e57af3a97> in <module>()
----> 1 next(x)
StopIteration:
In [7]: y
Out[7]: (True, <itertools._grouper at 0x10a672e80>)
In [8]: list(y[1])
Out[8]: [9]
Run Code Online (Sandbox Code Playgroud)
预期的产出list(y[1])是[0,1,2,3,4,5,6,7,8,9]
这里发生了什么?
我观察到这一点cpython 3.4.2,但其他人已经看到了这个cpython 3.5和IronPython 2.9.9a0 (2.9.0.0) on Mono 4.0.30319.17020 (64-bit).
观察到的行为Jython 2.7.0和pypy:
Python 2.7.10 (5f8302b8bf9f, Nov 18 2015, 10:46:46)
[PyPy 4.0.1 with GCC 4.8.4] …Run Code Online (Sandbox Code Playgroud) python python-2.x python-itertools python-3.x python-internals
python ×7
python-3.x ×2
algorithm ×1
analysis ×1
applescript ×1
chat ×1
dictionary ×1
equality ×1
hash ×1
java ×1
linux ×1
linux-kernel ×1
list ×1
macos ×1
performance ×1
perl ×1
python-2.x ×1
scheduling ×1
shelve ×1
split ×1
string ×1
timing ×1
tradeoff ×1
web-services ×1
window ×1