编写更高效的代码 [英] Writing more efficient code

查看:60
本文介绍了编写更高效的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,祝大家新年快乐。


我几周前选择了Python,并且能够解析大量的b $ b文件并处理数据非常容易,但我相信我的代码不是太高效了。我希望字典可以提供帮助,但我不确定

实现它们的最佳方式。


我一直在使用一堆嵌套的if / elif / else语句从文件中选择

切片(0317& 03de),然后解析数据(aa,hh,bb,

d2-d9)为参数(a =空速,h =航向)&标志。


#sample文件内容

0000007d 03 0317 aa aa aa aa hh hh hh bb bb

0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

#some pseudo code

如果PID ==''03de'':

flapsCmd = int(d3,16)

如果flapsCmd == 0xc0:

< flaps up code>

elif flapsCmd == 0x03:

< flaps down code>

如果PID ==''0317'':

airspeed ==''结合aa为空速和空速乘以0.1''

标题==''结合hh标题''

mach ==''结合bb for mach&乘以0.01''


在这种情况下,可能有字典帮助...比如Label0317(parameterName,

slice(d3),scaleFactor(0.1))。 ..如果他们愿意用b $ b替换几十个嵌套条件,我想用它们。我有大约75

不同的参数来解码一个包含~250万

行数据的文件。


我知道我的伪代码缺乏细节,但希望我得到我的

点...


(我认为switch / select / case语句有助于有点,但是python

似乎没有使用它们......不要发动宗教战争或任何事情。)


任何帮助(或鼓励)我很感激。

解决方案

gonzlobo写道:


我拿起了一些Python几个星期前,已经能够很容易地解析大量的b $ b文件并处理数据了,但我相信我的代码并不是很高效。我希望字典可以提供帮助,但我不确定

实现它们的最佳方式。


我一直在使用一堆嵌套的if / elif / else语句从文件中选择

切片(0317& 03de),然后解析数据(aa,hh,bb,

d2-d9)为参数(a =空速,h =航向)&标志。


#sample文件内容

0000007d 03 0317 aa aa aa aa hh hh hh bb bb

0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

#some pseudo code

如果PID ==''03de'':

flapsCmd = int(d3,16)

如果flapsCmd == 0xc0:

< flaps up code>

elif flapsCmd == 0x03:

< flaps down code>

如果PID ==''0317'':

airspeed ==''结合aa为空速和空速乘以0.1''

标题==''结合hh标题''

mach ==''结合bb for mach&乘以0.01''


在这种情况下,可能有字典帮助...比如Label0317(parameterName,

slice(d3),scaleFactor(0.1))。 ..如果他们愿意用b $ b替换几十个嵌套条件,我想用它们。



听起来你想要在列表上进行模式匹配:


| 0x03 :: 0xde :: 0xc0 :: t - < flaps up code>

| 0x03 :: 0xde :: 0x03 :: t - < flaps down code>

| 0x03 :: 0x17 :: t -...

这是OCaml代码,它以字节序列的形式查找模式,就像

那样你''正在寻找。


我有大约75

不同的参数来解码一个包含~250万美元的文件

数据行。


我知道我的伪代码缺乏细节,但希望我得到我的

点...


(我认为switch / select / case语句会有所帮助,但python

似乎没有使用它们......不要开始宗教战争或任何事情)。



模式匹配是切换类固醇。


OCaml的模式匹配器非常复杂且非常快。你可能会把你的代码缩小几倍,同时还能让它运行几个额外的b / b
更快并且静态检查,所以它会更多

可靠。


您可能希望查看模式匹配的任何语言:OCaml,SML,

Haskell,F#,Mathematica如果您正在运行Windows,那么F#是一种.NET

语言...


-

博士Jon D Harrop,飞蛙咨询公司

科学家的客观CAML
http://www.ffconsultancy.com/product...ex.html?usenet


Jon Harrop:


OCaml的模式匹配器非常复杂且非常快。你可能会把你的代码缩小几倍,同时还能让它运行几个额外的b / b
更快并且静态检查,所以它会更多

可靠。



你似乎忘记了几个月的学习OCaml的时间。

我的Python程序虽然不稳定但看起来足够可靠

已检查:-)


您可能希望查看具有模式匹配的任何语言:OCaml,SML,

Haskell,F#,Mathematica等。



Mathematica模式匹配很好,但Mathematica花了很多钱b $ b(你需要的)有一段时间学习它,它不是一个简单的系统。


再见,

熊宝宝


gonzlobo schrieb:


Greetings,happyNewYear to all。


我选择了Python几个星期前,已经能够很容易地解析大量的b $ b文件并处理数据了,但我相信我的代码并不是很有效。我希望字典可以提供帮助,但我不确定

实现它们的最佳方式。


我一直在使用一堆嵌套的if / elif / else语句从文件中选择

切片(0317& 03de),然后解析数据(aa,hh,bb,

d2-d9)为参数(a =空速,h =航向)&标志。


#sample文件内容

0000007d 03 0317 aa aa aa aa hh hh hh bb bb

0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

#some pseudo code

如果PID ==''03de'':

flapsCmd = int(d3,16)

如果flapsCmd == 0xc0:

< flaps up code>

elif flapsCmd == 0x03:

< flaps down code>

如果PID ==''0317'':

airspeed ==''结合aa为空速和空速乘以0.1''

标题==''结合hh标题''

mach ==''结合bb for mach&乘以0.01''


在这种情况下,可能有字典帮助...比如Label0317(parameterName,

slice(d3),scaleFactor(0.1))。 ..如果他们愿意用b $ b替换几十个嵌套条件,我想用它们。我有大约75

不同的参数来解码一个包含~250万

行数据的文件。


我知道我的伪代码缺乏细节,但希望我得到我的

点...


(我认为switch / select / case语句有助于有点,但是python

似乎没有使用它们......不要发动宗教战争或任何事情)。



字典方法肯定会更快 - 查询应该是/ b $ b通常是O(1)而不是O(n)对于if- elif-chain。


Diez


Greetings, and happyNewYear to all.

I picked up Python a few weeks ago, and have been able to parse large
files and process data pretty easily, but I believe my code isn''t too
efficient. I''m hoping dictionaries will help out, but I''m not sure the
best way to implement them.

I''ve been using a bunch of nested if/elif/else statements to select
slices (0317 & 03de) from a file, then parse the data (aa, hh, bb,
d2-d9) into parameters (a = airspeed, h = heading) & flags.

#sample file contents
0000007d 03 0317 aa aa aa aa aa hh hh hh bb bb
0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

# some pseudo code
if PID == ''03de'':
flapsCmd = int(d3, 16)
if flapsCmd == 0xc0:
<flaps up code>
elif flapsCmd == 0x03:
<flaps down code>
if PID == ''0317'':
airspeed == ''combine aa for airspeed & multiply by 0.1''
heading == ''combine hh for heading''
mach == ''combine bb for mach & multiply by 0.01''

Might dictionaries help in this case... say Label0317(parameterName,
slice (d3), scaleFactor(0.1))... I''d like to use them if they''ll
replace the dozens of nested conditionals. I have roughly 75
different parameters to decode from a file containing ~2.5 million
lines of data.

I know my pseudo code lacks details, but hopefully I''m getting my
point across...

(I suppose switch/select/case statements would help a bit, but python
doesn''t seem to use them... not to start a religious war or anything).

Any help (or encouragement) is appreciated.

解决方案

gonzlobo wrote:

I picked up Python a few weeks ago, and have been able to parse large
files and process data pretty easily, but I believe my code isn''t too
efficient. I''m hoping dictionaries will help out, but I''m not sure the
best way to implement them.

I''ve been using a bunch of nested if/elif/else statements to select
slices (0317 & 03de) from a file, then parse the data (aa, hh, bb,
d2-d9) into parameters (a = airspeed, h = heading) & flags.

#sample file contents
0000007d 03 0317 aa aa aa aa aa hh hh hh bb bb
0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

# some pseudo code
if PID == ''03de'':
flapsCmd = int(d3, 16)
if flapsCmd == 0xc0:
<flaps up code>
elif flapsCmd == 0x03:
<flaps down code>
if PID == ''0317'':
airspeed == ''combine aa for airspeed & multiply by 0.1''
heading == ''combine hh for heading''
mach == ''combine bb for mach & multiply by 0.01''

Might dictionaries help in this case... say Label0317(parameterName,
slice (d3), scaleFactor(0.1))... I''d like to use them if they''ll
replace the dozens of nested conditionals.

Sounds like you want pattern matching over lists:

| 0x03 :: 0xde :: 0xc0 :: t -<flaps up code>
| 0x03 :: 0xde :: 0x03 :: t -<flaps down code>
| 0x03 :: 0x17 :: t -...

That''s OCaml code which finds patterns in sequences of bytes, just like the
ones you''re searching for.

I have roughly 75
different parameters to decode from a file containing ~2.5 million
lines of data.

I know my pseudo code lacks details, but hopefully I''m getting my
point across...

(I suppose switch/select/case statements would help a bit, but python
doesn''t seem to use them... not to start a religious war or anything).

Pattern matching is "switch" on steroids.

OCaml''s pattern matcher is very sophisticated and very fast. You''ll probably
shrink your code by several fold whilst also having it run a few orders of
magnitude faster and having it statically checked, so it will be more
reliable.

You might want to look at any language with pattern matching: OCaml, SML,
Haskell, F#, Mathematica etc. If you''re running Windows then F# is a .NET
language...

--
Dr Jon D Harrop, Flying Frog Consultancy
Objective CAML for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet


Jon Harrop:

OCaml''s pattern matcher is very sophisticated and very fast. You''ll probably
shrink your code by several fold whilst also having it run a few orders of
magnitude faster and having it statically checked, so it will be more
reliable.

You seem to forget some months of time to learn OCaml.
And my Python programs seem reliable enough despite being unstatically
checked :-)

You might want to look at any language with pattern matching: OCaml, SML,
Haskell, F#, Mathematica etc.

Mathematica pattern matching is good, but Mathematica costs a lot of
money (and you need some time to learn it, it''s not an easy system).

Bye,
bearophile


gonzlobo schrieb:

Greetings, and happyNewYear to all.

I picked up Python a few weeks ago, and have been able to parse large
files and process data pretty easily, but I believe my code isn''t too
efficient. I''m hoping dictionaries will help out, but I''m not sure the
best way to implement them.

I''ve been using a bunch of nested if/elif/else statements to select
slices (0317 & 03de) from a file, then parse the data (aa, hh, bb,
d2-d9) into parameters (a = airspeed, h = heading) & flags.

#sample file contents
0000007d 03 0317 aa aa aa aa aa hh hh hh bb bb
0000007e 06 03de d2 d3 d4 d5 d6 d7 d8 d9 10 11

# some pseudo code
if PID == ''03de'':
flapsCmd = int(d3, 16)
if flapsCmd == 0xc0:
<flaps up code>
elif flapsCmd == 0x03:
<flaps down code>
if PID == ''0317'':
airspeed == ''combine aa for airspeed & multiply by 0.1''
heading == ''combine hh for heading''
mach == ''combine bb for mach & multiply by 0.01''

Might dictionaries help in this case... say Label0317(parameterName,
slice (d3), scaleFactor(0.1))... I''d like to use them if they''ll
replace the dozens of nested conditionals. I have roughly 75
different parameters to decode from a file containing ~2.5 million
lines of data.

I know my pseudo code lacks details, but hopefully I''m getting my
point across...

(I suppose switch/select/case statements would help a bit, but python
doesn''t seem to use them... not to start a religious war or anything).

The dictionary approach certainly will be speedier - lookup should
usually be O(1) instead of O(n) for the if-elif-chain.

Diez


这篇关于编写更高效的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆