#ifdef #ifndef 在 Java 中 [英] #ifdef #ifndef in Java

查看:24
本文介绍了#ifdef #ifndef 在 Java 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑是否有办法在 Java 中创建编译时条件,如 C++ 中的 #ifdef #ifndef.

I doubt if there is a way to make compile-time conditions in Java like #ifdef #ifndef in C++.

我的问题是有一个用 Java 编写的算法,并且我对该算法有不同的运行时间改进.所以我想衡量每次使用改进时我节省了多少时间.

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.

现在我有一组布尔变量,用于在运行期间决定应该使用哪些改进,哪些不使用.但即使测试这些变量也会影响总运行时间.

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.

有人知道用 Java 实现的方法吗?或者也许有人知道没有这样的方法(这也很有用).

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).

推荐答案

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"));

然后任何依赖于 enableFast 的条件都将由 JIT 编译器进行评估.这方面的开销可以忽略不计.

Then any conditions dependent on enableFast will be evaluated by the JIT compiler. The overhead for this is negligible.

这篇关于#ifdef #ifndef 在 Java 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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