小编Amb*_*wal的帖子

有没有办法在C中使用带常量索引的常量数组作为开关案例标签?

我有一些常量值和数组定义他们的标签和他们的哈希码.例如,

#define LABEL_A 0 //or const int LABEL_A = 0;
#define LABEL_B 1
#define LABEL_C 2
#define LABEL_D 3

const char *VALUE[] = {"LABEL_A", "LABEL_B", "LABEL_C", "LABEL_D"};
const int VALUE_HASH[] = {67490, 67491, 67493, 67459);
Run Code Online (Sandbox Code Playgroud)

在运行时,这些标签可以按任何顺序排列,需要进行相应的解析.我正在使用开关盒来达到这个目的.此代码在编译时生成错误"需要常量表达式.

function(const char* LabelAtRuntime){
  int i = getHashCode(LabelAtRuntime);
  switch(i){
    case VALUE_HASH[LABEL_A]: //line giving compile time error
      break;
    default:
      break;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我提供实际常量时,它是有效的.这段代码效果很好.

function(const char* LabelAtRuntime){
  int i = getHashCode(LabelAtRuntime);
  switch(i){
    case 67490: //line not giving compile time error
      break;
    default:
      break;
}
Run Code Online (Sandbox Code Playgroud)
  1. 我无法理解,为什么会这样?我的数组和它的索引都是常量,那么它不等同于常量文字吗?
  2. 有没有其他方法可以按要求的方式提供常量? …

c arrays switch-statement

9
推荐指数
2
解决办法
1201
查看次数

32位gcc和64位gcc与-m32选项有什么区别?

我的团队最近一直在研究JNI,因此我们遇到了有关32位和64位架构的不同问题.我们来举个例子(temp.c).

#include <stdio.h>
void main(){
  printf("long=%d\n",sizeof(long));
}
Run Code Online (Sandbox Code Playgroud)

有什么区别gcc_32_bit temp.cgcc_64_bit -m32 temp.c

案例测试:

案例1:使用64位ubuntu编译的代码gcc temp.c.输出: long=8在64位ubuntu上.

案例2:使用64位ubuntu编译的代码gcc -m32 temp.c.输出: long=4在64位ubuntu上.

案例3:使用64位MAC(使用64位交叉编译器)编译的代码/usr/local/gcc-4.8.1-for-linux64/bin/x86_64-pc-linux-gcc temp.c.输出: long=8在64位ubuntu上.

案例4:使用64位MAC(使用32位交叉编译器)编译的代码/usr/local/gcc-4.8.1-for-linux32/bin/i586-pc-linux-gcc temp.c.输出: Error: cannot run在64位ubuntu上.

我们使用MAC交叉编译器,因为我们没有32位linux机器.

c macos ubuntu cpu-architecture cross-compiling

5
推荐指数
1
解决办法
1226
查看次数

为什么System.out :: println比Java 8中的匿名类实现慢?

我正在使用一些Java 8 StreamAPI.我很困惑看到以下两个解决方案之间的性能差异,即只打印内容Stream.

解决方案1:

int[] array = new int[] { 0, 1, 2, 3, 4, 5 };
start = System.nanoTime();
Arrays.stream(array).forEach(System.out::println);
System.out.println((System.nanoTime() - start) / 1000000.0f);
Run Code Online (Sandbox Code Playgroud)

解决方案2:

int[] array = new int[] { 0, 1, 2, 3, 4, 5 };
start = System.nanoTime();
Arrays.stream(array).forEach(new IntConsumer() {
    @Override
    public void accept(int value) {
        System.out.println(value);
    }
});
System.out.println((System.nanoTime() - start) / 1000000.0f);
Run Code Online (Sandbox Code Playgroud)

执行时,Solution 1约需要 比时间长5-6倍Solution 2.

系统配置:

  • JRE: 1.8.0_101 64 bit
  • OS: Windows 10 Home …

java performance consumer java-8 java-stream

0
推荐指数
1
解决办法
231
查看次数