Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Java和C中的字符串比较性能_String_Performance_Time_Compare_Execution - Fatal编程技术网

String Java和C中的字符串比较性能

String Java和C中的字符串比较性能,string,performance,time,compare,execution,String,Performance,Time,Compare,Execution,我需要测量比较两个字符串的函数的性能。我的任务是用Java和C编写它,并比较执行时间。出于测试目的,我生成了一个包含100000个随机字符串的txt文件,每个字符串的长度从100到200个字符不等。使用它们,我调用比较函数20'000'000次。在Java中大约需要500毫秒,而在C中执行时间是0毫秒,我在两种语言中对相同的数据进行完全相同的测试。即使我将C中的呼叫数增加到20000,它的持续时间仍然是0毫秒。怎么可能呢?我错过了什么重要的事情吗 Java中的实现 这是我用来测试C语言性能的代码

我需要测量比较两个字符串的函数的性能。我的任务是用Java和C编写它,并比较执行时间。出于测试目的,我生成了一个包含100000个随机字符串的txt文件,每个字符串的长度从100到200个字符不等。使用它们,我调用比较函数20'000'000次。在Java中大约需要500毫秒,而在C中执行时间是0毫秒,我在两种语言中对相同的数据进行完全相同的测试。即使我将C中的呼叫数增加到20000,它的持续时间仍然是0毫秒。怎么可能呢?我错过了什么重要的事情吗

Java中的实现

这是我用来测试C语言性能的代码


您的代码的可观察行为与根本不做任何事情的代码完全相同。您需要将字符串比较的结果作为程序可观察行为的一部分,以便它们不能被优化为零。尝试统计字符串匹配的次数和字符串不匹配的次数,然后输出该数字

public class StringComparer {

    public static boolean compareStrings(String string1, String string2) {

        if(string1.length() != string2.length()) {
            return false;
        }

        for (int i = 0; i < string1.length(); i++) {
            if(string1.charAt(i) != string2.charAt(i)) {
                return false;
            }
        }

        return true;
    }
}
bool string_compare(char* s1, char* s2)
{   
    int i = 0;

    while (s1[i] != NULL && s1[i] == s2[i])
        i++;

    return s1[i] == s2[i];
}
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <windows.h>

#define NUMBER_OF_WORDS 100000
#define MAX_WORD_LENGTH 200

long long milliseconds_now() {
    static LARGE_INTEGER s_frequency;
    BOOL s_use_qpc = QueryPerformanceFrequency(&s_frequency);
    if (s_use_qpc) {
        LARGE_INTEGER now;
        QueryPerformanceCounter(&now);
        return (1000LL * now.QuadPart) / s_frequency.QuadPart;
    }
    else {
        return GetTickCount();
    }
}

int main()
{
    char* fileName = "tests.txt";
    FILE *file = fopen(fileName, "r");

    char* words[NUMBER_OF_WORDS];

    long long i, j;

    for (i = 0; i < NUMBER_OF_WORDS; i++) {
        words[i] = (char*)malloc((MAX_WORD_LENGTH + 1) * sizeof(char));
        fgets(words[i], MAX_WORD_LENGTH + 1, file);
    }

    long long repeats = 10000000000 / NUMBER_OF_WORDS;

    long long start = milliseconds_now();

    for (i = 0; i < repeats; i++)
    {
        for (j = 0; j < NUMBER_OF_WORDS - 1; j++)
        {
            ;
        }
    }

    long long loopDuration = milliseconds_now() - start;

    start = milliseconds_now();

    for (i = 0; i < repeats; i++)
    {
        for (j = 0; j < NUMBER_OF_WORDS - 1; j++)
        {
            string_compare(words[j], words[j + 1]); //compare different strings
            string_compare(words[j], words[j]); //compare the same strings
        }
    }

    long long customFunctionDuration = milliseconds_now() - start;

    printf("Loop duration: %lld\n", loopDuration);
    printf("Custom function duration: %lld - %lld = %lld ms", customFunctionDuration, loopDuration, customFunctionDuration - loopDuration);

    return 0;
}