#ifdef #ifndef 在 Java 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1813853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#ifdef #ifndef in Java
提问by jutky
I doubt if there is a way to make compile-time conditions in Java like #ifdef #ifndef in C++.
我怀疑是否有办法在 Java 中创建编译时条件,如 C++ 中的 #ifdef #ifndef。
My problem is that have an algorithm written in Java, and I have different running time improves to that algorithm. So I want to measure how much time I save when each improve is used.
我的问题是有一个用 Java 编写的算法,并且我对该算法有不同的运行时间改进。所以我想衡量使用每个改进时我节省了多少时间。
Right now I have a set of boolean variables that are used to decide during the running time which improve should be used and which not. But even testing those variables influences the total running time.
现在我有一组布尔变量,用于在运行期间决定应该使用哪些改进,哪些不使用。但即使测试这些变量也会影响总运行时间。
So I want to find out a way to decide during the compilation time which parts of the program should be compiled and used.
所以我想找到一种方法来决定在编译期间应该编译和使用程序的哪些部分。
Does someone knows a way to do it in Java. Or maybe someone knows that there is no such way (it also would be useful).
有人知道用 Java 实现的方法吗?或者也许有人知道没有这样的方法(它也很有用)。
采纳答案by Mark Thornton
private static final boolean enableFast = false;
// ...
if (enableFast) {
// This is removed at compile time
}
Conditionals like that shown above are evaluated at compile time. If instead you use this
上面显示的条件是在编译时评估的。如果你使用这个
private static final boolean enableFast = "true".equals(System.getProperty("fast"));
Then any conditions dependent on enableFast will be evaluated by the JIT compiler. The overhead for this is negligible.
然后任何依赖于 enableFast 的条件都将由 JIT 编译器评估。这方面的开销可以忽略不计。
回答by Tom
Never used it, but this exists
没用过,但是有这个
JCPP is a complete, compliant, standalone, pure Java implementation of the C preprocessor. It is intended to be of use to people writing C-style compilers in Java using tools like sablecc, antlr, JLex, CUP and so forth. This project has has been used to successfully preprocess much of the source code of the GNU C library. As of version 1.2.5, it can also preprocess the Apple Objective C library.
JCPP 是 C 预处理器的完整、兼容、独立、纯 Java 实现。它旨在用于使用 sablecc、antlr、JLex、CUP 等工具在 Java 中编写 C 风格编译器的人。该项目已被用于成功预处理 GNU C 库的大部分源代码。从 1.2.5 版本开始,它还可以预处理 Apple Objective C 库。
回答by jldupont
Use the Factory Pattern to switch between implementations of a class?
使用工厂模式在类的实现之间切换?
The object creation time can't be a concern now could it? When averaged over a long running time period, the biggest component of time spent should be in the main algorithm now wouldn't it?
对象创建时间现在不能成为问题吗?当在长时间运行时平均时,花费的时间的最大组成部分现在应该在主算法中,不是吗?
Strictly speaking, you don't really need a preprocessor to do what you seek to achieve. There are most probably other ways of meeting your requirement than the one I have proposed of course.
严格来说,您实际上并不需要预处理器来完成您想要实现的目标。当然,除了我提出的方法之外,很可能还有其他方法可以满足您的要求。
回答by Phil Ross
javac will not output compiled code that is unreachable. Use a final variable set to a constant value for your #define
and a normal if
statement for the #ifdef
.
javac 不会输出无法访问的已编译代码。使用最后的变量设置为你的恒定值#define
和正常if
的声明#ifdef
。
You can use javap to prove that the unreachable code isn't included in the output class file. For example, consider the following code:
您可以使用 javap 来证明无法访问的代码未包含在输出类文件中。例如,考虑以下代码:
public class Test
{
private static final boolean debug = false;
public static void main(String[] args)
{
if (debug)
{
System.out.println("debug was enabled");
}
else
{
System.out.println("debug was not enabled");
}
}
}
javap -c Test
gives the following output, indicating that only one of the two paths was compiled in (and the if statement wasn't):
javap -c Test
给出以下输出,表明仅编译了两个路径之一(并且没有编译 if 语句):
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String debug was not enabled
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
回答by jutky
I think that I've found the solution, It's much simpler.
If I define the boolean variables with "final" modifier Java compiler itself solves the problem. Because it knows in advance what would be the result of testing this condition.
For example this code:
我想我已经找到了解决方案,它简单得多。
如果我用“final”修饰符定义布尔变量,Java 编译器本身就解决了这个问题。因为它事先知道测试这个条件的结果是什么。例如这段代码:
boolean flag1 = true;
boolean flag2 = false;
int j=0;
for(int i=0;i<1000000000;i++){
if(flag1)
if(flag2)
j++;
else
j++;
else
if(flag2)
j++;
else
j++;
}
runs about 3 seconds on my computer.
And this one
在我的电脑上运行大约 3 秒。
和这个
final boolean flag1 = true;
final boolean flag2 = false;
int j=0;
for(int i=0;i<1000000000;i++){
if(flag1)
if(flag2)
j++;
else
j++;
else
if(flag2)
j++;
else
j++;
}
runs about 1 second. The same time this code takes
运行约 1 秒。这段代码需要的时间
int j=0;
for(int i=0;i<1000000000;i++){
j++;
}
回答by rustyx
If you really need conditional compilation and you use Ant, you might be able to filter your code and do a search-and-replace in it.
如果您确实需要条件编译并且您使用Ant,您可能能够过滤您的代码并在其中进行搜索和替换。
For example: http://weblogs.java.net/blog/schaefa/archive/2005/01/how_to_do_condi.html
例如:http: //weblogs.java.net/blog/schaefa/archive/2005/01/how_to_do_condi.html
In the same manner you can, for example, write a filter to replace LOG.debug(...);
with /*LOG.debug(...);*/
. This would still execute faster than if (LOG.isDebugEnabled()) { ... }
stuff, not to mention being more concise at the same time.
以同样的方式,你可以,例如,写一个过滤器,以取代LOG.debug(...);
用/*LOG.debug(...);*/
。这仍然会比if (LOG.isDebugEnabled()) { ... }
东西执行得更快,更不用说同时更简洁了。
If you use Maven, there is a similar feature described here.
如果您使用Maven,这里描述了一个类似的功能。
回答by alicanbatur
final static int appFlags = context.getApplicationInfo().flags;
final static boolean isDebug = (appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0