我想分割一个字符串,其中包含两个单词,如"word1 word2",使用分割和分区,并分别打印(使用for)单词,如:
Partition:
word1
word2
Split:
word1
word2
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
print("Hello World")
name = raw_input("Type your name: ")
train = 1,2
train1 = 1,2
print("Separation with partition: ")
for i in train1:
print name.partition(" ")
print("Separation with split: ")
for i in train1:
print name.split(" ")
Run Code Online (Sandbox Code Playgroud)
发生这种情况:
Separation with partition:
('word1', ' ', 'word2')
('word1', ' ', 'word2')
Separation with split:
['word1', 'word2']
['word1', 'word2']
Run Code Online (Sandbox Code Playgroud) 我一直在寻找这个基于性能的问题的答案.
到目前为止,在挖掘了互联网之后,我了解到有几种方法可以在java中使用Enums,这里有详细记载.好吧,作为一个初学者,我希望在switch-case语句中使用Enums ,这样可以提供清晰度并更好地理解代码.但另一方面,我们还有一个访问者模式样式的Enums实现,它确保了类型的安全性和可扩展性,这里讨论.
话虽如此,并回到这个问题背后的原始想法,到目前为止我已经了解到如果使用Enums正确设计switch-case结构,这确保了case值不稀疏,并且Enum声明是相同的编译单元作为switch-case语句,java编译器通过实现跳转表这样的构造对生成的字节码执行一些优化(在这里和其他地方讨论过,在Sun的网站上,我丢失了链接).现在,与多个/嵌套的if-else构造相比,这肯定会提升性能.
我的问题是,java如何在生成的字节码中实现基于访问者模式的Enums实现,与基于switch-case的实现(如果有的话)相比,性能提升了多少?
我应该选择哪种类型的实现,考虑到我的Enums可能在未来增长,我也热衷于性能.目前,我的Enum中有一些19和奇数常量.
编辑
我有一个类存储有关游戏变量的一些信息.其中一个变量是Enum类型.
public class GameObject {
private Shape mShape;
public Shape getShape() {
return mShape;
}
.
.
.
public static enum Shape {
SHAPE1, SHAPE2, SHAPE3, SHAPE4, ..., SHAPE20
};
public void drawShape() {
switch (this.mShape) {
case SHAPE1:
drawShape1();
break;
case SHAPE2:
drawShape2();
break;
case SHAPE3:
drawShape3();
break;
case SHAPE4:
drawShape4();
break;
.
.
.
. …Run Code Online (Sandbox Code Playgroud) 类似的问题(但答案对我不起作用):如何取消使用 concurrent.futures.ProcessPoolExecutor 运行的长时间运行的子进程?
与上面链接的问题和提供的解决方案不同,在我的情况下,计算本身相当长(受 CPU 限制)并且无法循环运行以检查是否发生了某些事件。
以下代码的简化版本:
import asyncio
import concurrent.futures as futures
import time
class Simulator:
def __init__(self):
self._loop = None
self._lmz_executor = None
self._tasks = []
self._max_execution_time = time.monotonic() + 60
self._long_running_tasks = []
def initialise(self):
# Initialise the main asyncio loop
self._loop = asyncio.get_event_loop()
self._loop.set_default_executor(
futures.ThreadPoolExecutor(max_workers=3))
# Run separate processes of long computation task
self._lmz_executor = futures.ProcessPoolExecutor(max_workers=3)
def run(self):
self._tasks.extend(
[self.bot_reasoning_loop(bot_id) for bot_id in [1, 2, 3]]
)
try:
# Gather bot reasoner tasks
_reasoner_tasks = …Run Code Online (Sandbox Code Playgroud) python subprocess python-asyncio python-multiprocessing process-pool
我正在尝试复制 Mikhail Andreev 与此处发布的 Django 频道的聊天:https : //gearheart.io/blog/creating-a-chat-with-django-channels/
当我运行服务器时:$ python3 ./manage.py runserverredis 服务器没有启动。这是完整的消息:
Performing system checks...
System check identified no issues (0 silenced).
April 27, 2017 - 20:59:01
Django version 1.10.3, using settings 'multichat.settings'
Starting Channels development server at http://127.0.0.1:8000/
Channel layer default (asgi_redis.core.RedisChannelLayer)
Quit the server with CONTROL-C.
2017-04-27 20:59:01,278 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2017-04-27 20:59:01,279 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.receive
2017-04-27 20:59:01,282 …Run Code Online (Sandbox Code Playgroud) python ×3
channels ×1
django ×1
enums ×1
java ×1
partition ×1
performance ×1
process-pool ×1
python-2.7 ×1
redis ×1
split ×1
string ×1
subprocess ×1