在 Julia 中,为什么 @printf 是宏而不是函数? [英] In Julia, why is @printf a macro instead of a function?

查看:21
本文介绍了在 Julia 中,为什么 @printf 是宏而不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Julia 中,打印格式化字符串的语法如下:

In Julia, the syntax to print a formatted string is as follows:

@printf("Hello %d
", 5)

为什么 @printf 是宏而不是函数?是否可以接受不同数量的参数?

Why is @printf a macro instead of a function? Is it so that it can accept a varying number of arguments?

推荐答案

对于普通的 Julia 函数来说,采用可变数量的参数不是问题 [1].@printf 是一个宏,因此它可以在编译时解析和解释格式字符串,并为该特定格式字符串生成自定义代码.人们可能没有意识到,每次调用 printf 时,C 的 printf 函数都会重新解析和重新解释格式字符串.它和它一样快的事实代表了疯狂指针编程的一个小奇迹.说真的,只要看看离你最近的 libc 的 printf 实现.这完全是疯了.

Taking a variable number of arguments is not a problem for normal Julia functions [1]. @printf is a macro so that it can parse and interpret the format string at compile time and generate custom code for that specific format string. People may not realize that C's printf function re-parses and re-interprets the format string each time you call printf. The fact that it's as fast as it is represents a minor miracle of insane pointer programming. Seriously, just look at your nearest libc's printf implementation. It's completely nuts.

Julia 使用不同的方法:@printf 是一个宏,可将格式字符串转换为特定于该格式规范的高效代码.如果您考虑一下,printf 样式的格式字符串实际上只是一种表达函数的方式,该函数采用固定数量和类型的参数并以特定方式打印它们.请注意,我说 format string 是一个函数,而不是 printf 本身,它在概念上是一个函数生成器,将格式转换为格式化程序.这一切都被塞进了 C 中的运行时函数这一事实有点不匹配,因为这是 C 中唯一合理的选择.事实上,正因为如此,直到最近,你还是很容易自爆通过将错误数量或类型的参数传递给 C 的 printf 在脚上.这只是现在更好,因为编译器已经被特殊情况下理解 printf 格式的语义.

Julia uses a different approach: @printf is a macro which translates format strings into efficient code specific to that format specification. If you think about it, a printf-style format string is really just a way to express a function that takes a fixed number and type of arguments and prints them in a particular way. Note that I said that the format string is a function, not printf itself, which is conceptually a function generator, turning formats into formatters. The fact that this is all crammed into a run-time function in C is a bit of a mismatch due to that being the only reasonable option in C. In fact, because of this, until very recently, it was rather easy to shoot yourself in the foot by passing the wrong number or type of arguments to C's printf. This is only better now because compilers have been special-cased to understand the semantics of printf formats.

理论上,Julia 的 @printf 可以比 C 更快,因为它生成自定义代码,但实际上,我很难匹配 C,更不用说击败它了.但我认为这是由于我们 I/O 系统的当前设计以及我如何使用它,而不是固有的限制.不过,I/O 的内容应该进行大修,当这种情况发生时,我们实际上可以利用 @printf 是一个宏这一事实在格式化打印方面击败 C.

In theory, Julia's @printf can be made faster than C since it generate's custom code, but in practice, I had a hard enough time matching C, let alone beating it. But I think that's due to the current design of our I/O system and how I'm using it, not an inherent limitation. The I/O stuff is due for an overhaul though, and when that happens, we might actually be able to beat C at formatted printing by leveraging the fact that @printf is a macro.

这篇关于在 Julia 中,为什么 @printf 是宏而不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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