相当于 Python 的 C 预处理器宏

时间:2023-04-28
本文介绍了相当于 Python 的 C 预处理器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我用来在 C 中定义宏(不仅仅是常量)

I use to define macros (not just constants) in C like

#define loop(i,a,b) for(i=a; i<b; ++i)
#define long_f(a,b,c) (a*0.123 + a*b*5.6 - 0.235*c + 7.23*c - 5*a*a + 1.5)

有没有办法在 python 中使用 preprocess 而不是函数?

Is there a way of doing this in python using a preprocess instead of a function?

*通过 preprocess 我的意思是在运行代码之前替换定义的出现(实际上不是整个代码而是其余代码,因为它是代码的一部分,我猜它将在运行时替换所有内容).

*By preprocess I mean something that replaces the occurrences of the definition before running the code (actually not the whole code but the rest of the code, because since it's part of the code, I guess it will replace everything during runtime).

如果有,值得吗?运行时间会有显着差异吗?

If there is, worth it? Will there be a significant difference in run time?

推荐答案

好吧,如果您要执行一些可以提前执行的非常困难的计算,那么,也许这是有道理的:通常用户会更开心使用快程序而不是慢程序.

Well, if you're going to perform some really hard calculations that could be performed in advance, then, perhaps, this makes sense: usually users are more happy with fast programs rather than slow ones.

但是,就原始性能"(即算术计算的速度)而言,恐怕 python 不是一个好的选择.至少如果我们谈论标准的 Python 实现,称为 CPython.

But, I'm afraid python isn't a good choice when it comes to 'raw performance', that is, speed of arithmetic calculations. At least if we talk about the standard python implementation, called CPython.

或者,您可以检查其他变体:

Alternatively, you could check other variants:

  • PyPy.这是纯 Python 中的另一种 Python 实现.得益于 JIT 编译器,它提供了更好的性能,但需要更多的内存.
  • Cython.这是 Python 的一个扩展,它允许人们[方便地]为代码的性能关键部分创建可编译的片段.
  • 使用您喜欢的任何外部预处理器.M4 和 FilePP 是我首先想到的,但有很多.
  • PyPy. This is an alternative python implementation, in pure Python. Thanks to a JIT compiler it gives better performance but requires a lot more memory.
  • Cython. This is an extension to Python, which allows one to [conveniently] create compileable snippets for perfomance critical parts of the code.
  • Use a whatever external pre-processor you like. M4 and FilePP are what come to my mind first, but there're plenty of them.

这篇关于相当于 Python 的 C 预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Jinja2 内联注释 下一篇:在 Python 中,如何在不更改命名空间的情况下在另

相关文章

最新文章