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

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

问题描述

我怀疑是否有一种方法可以使Java中的编译时条件像#ifdef #ifndef在C ++中。

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 in Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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