相关疑难解决方法(0)

测量Python中经过的时间?

我想要的是开始在我的代码中的某个地方计算时间,然后获得通过的时间,以测量执行少量功能所花费的时间.我认为我使用的是timeit模块错误,但文档对我来说只是让人困惑.

import timeit

start = timeit.timeit()
print("hello")
end = timeit.timeit()
print(end - start)
Run Code Online (Sandbox Code Playgroud)

python performance timeit measure

1031
推荐指数
32
解决办法
116万
查看次数

正确的写行到文件的方法?

我习惯了 print >>f, "hi there"

但是,它似乎print >>已被弃用.建议的方法是什么?

更新:关于所有这些答案"\n"......是通用的还是特定于Unix的?IE,我应该"\r\n"在Windows 上做什么?

python file-io

985
推荐指数
11
解决办法
196万
查看次数

通过添加'print'语句进行调试的正确名称是什么

有许多方法可以进行调试,使用调试器就是其中之一,但对于谦虚,懒惰的程序员而言,简单的方法就是在代码中添加一堆打印语句.

 def foo(x):
     print 'Hey wow, we got to foo!', x

     ...

     print 'foo is returning:', bar
     return bar
Run Code Online (Sandbox Code Playgroud)

这种调试方式是否有正确的名称?

debugging printf-debugging

33
推荐指数
9
解决办法
1万
查看次数

在Python中实时读取串行数据

我使用Python中的脚本通过串行端口以2Mbps从PIC单片机收集数据.

PIC工作在2Mbps的完美时序,FTDI usb串行端口也能在2Mbps下工作(均通过示波器验证)

我发送消息(大约15个字符大小)大约每秒100-150倍,并增加数量(以检查我是否有丢失的消息等)

在我的笔记本电脑上我有Xubuntu作为虚拟机运行,我可以通过Putty和我的脚本(python 2.7和pySerial)读取串口

问题:

  • 当通过Putty打开串口时,我看到所有消息(消息中的计数器逐1递增).完善!
  • 当通过pySerial打开串口时,我看到所有消息,但不是每秒接收100-150x,而是以每秒约5次的速度接收它们(仍然是消息逐渐递增1)但是它们可能存储在某个缓冲区中,就像我关机时一样PIC,我可以去厨房然后回来,我仍然收到消息.

这是代码(我省略了大部分代码,但循环是相同的):

ser = serial.Serial('/dev/ttyUSB0', 2000000, timeout=2, xonxoff=False, rtscts=False, dsrdtr=False) #Tried with and without the last 3 parameters, and also at 1Mbps, same happens.
ser.flushInput()
ser.flushOutput()
While True:
  data_raw = ser.readline()
  print(data_raw)
Run Code Online (Sandbox Code Playgroud)

任何人都知道为什么pySerial需要这么多时间从串口读取直到行尾?有帮助吗?

我想实时拥有这个.

谢谢

python serial-port pyserial python-2.7

33
推荐指数
3
解决办法
16万
查看次数

使用System.out和PrintWriter写入控制台

在阅读Java I/O时,我意识到有两种方法可以写入标准输出.

以下是使用这两种技术的代码段

import java.io.*;
public class ConsoleIO {

    public static void main(String[] args) {
        System.out.println("Method 1");

        PrintWriter writer = new PrintWriter(System.out);
        writer.println("Method 2");
        writer.flush();
        writer.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

使用一个比另一个有任何性能优势吗?

java io console performance

29
推荐指数
3
解决办法
6万
查看次数

python 将文本写入文件比使用 python 将文本打印到终端慢?

我正在编写一个程序,它接受一个字符串并从该字符串计算所有可能的重复排列。我将展示我的一些代码片段,如果有人能指点我如何提高将数据发送到文件时的速度,我将不胜感激。

场景一

将输出发送到 stdout 大约需要 12 秒才能写入 531,441 行 (3mb)

import itertools 
for word in itertools.product(abcdefghi,repeat = 6):
    print(word)
Run Code Online (Sandbox Code Playgroud)

场景二

然后我尝试将输出发送到文件而不是标准输出,这大约花了大约 5 分钟。

import itertools
word_counter=0
for word in itertools.product(abcdefghi,repeat = 6): 
    word_counter=word_counter+1
    if word_counter==1:
        open('myfile', 'w').write(word)
    else:
        open('myfile', 'a').write(word)
Run Code Online (Sandbox Code Playgroud)

word_counter在函数循环时跟踪重复排列的数量。当word_counter为 1 时,程序创建文件,然后在 word_counter 大于 1 时将数据附加到文件中。

我使用网络上的一个程序来执行此操作,我发现该程序在将数据打印到终端时花费了相同的时间,并且这个相同的网络程序花费了大约 3 秒将这些组合输出到文件中,而我的程序花费了 5 分钟来输出数据到文件!

我还尝试运行我的程序并将输出重定向到 bash 终端中的文件,这花费了相同的时间(3 秒)!

'myprog' > 'output file'
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
1759
查看次数

当我在 Python 中使用 stdout 时,Powershell 无法显示字符串?

它在 VS Code 中运行良好,但在 powershell 中它会提示用户输入,但不会在“stdout”中显示字符串。这是示例代码:

import sys


def get_int():
    sys.stdout.write("Enter number(s). ")
    return map(int, sys.stdin.readline().strip().split())


def get_float():
    sys.stdout.write("Enter number(s). ")
    return map(float, sys.stdin.readline().strip().split())


def get_list_of_int():
    sys.stdout.write("Enter numbers followed by space. ")
    return list(map(int, sys.stdin.readline().strip().split()))


def get_string():
    sys.stdout.write("Enter string. ")
    return sys.stdin.readline().strip()


a, b, c, d = get_int()
e, f, g = get_float()
arr = get_list_of_int()
str = get_string()
Run Code Online (Sandbox Code Playgroud)

python powershell

2
推荐指数
1
解决办法
120
查看次数

如何加速Python中的循环进度?

我的问题很简单.我有一个像这样的python简单代码:

for i in range(1,1193616,1) :
        print i
Run Code Online (Sandbox Code Playgroud)

因此,将打印范围1中的所有数字,直到1193616,这个循环进度需要很长时间..如何使其快速?

编辑:

实际上,我尝试为图像数据(Raster)制作一个A-star寻路程序.到目前为止,这是我的A-star函数脚本:

def A_star(h,c,dx,dy,u,s_id,e_id,Op,Cl,Prt,CC,o,ht,w):
    Op.append(s_id) 
    while e_id not in Op : 
        if Op == [ ] :
            break
        candidate = { }
        for i in Op :
            d = {i : CC[i]}
            candidate.update(d)
        o = min(candidate, key=candidate.get)
        Cl.append(o)
        Op.remove(o)
        adjacent_list = adjacent_cell(o,dx,dy )
        for p in adjacent_list :
            if p in Cl:       
                adjacent_list = filter(lambda i: i != p, adjacent_list)    
            elif p not in Op :   
                Op.append(p)
                d = {p : …
Run Code Online (Sandbox Code Playgroud)

python optimization loops

-1
推荐指数
1
解决办法
372
查看次数