SORU
8 EKİM 2012, PAZARTESİ


En hızlı dosya ise standart kullanan varsa kontrol etmek için C /C 11/C yolu?

Benim gibi bulmak için en hızlı yol için bir dosya var içinde standart c 11(veya)c (veya)c (elimde binlerce dosya ve yapmadan önce bir şey üzerinde kontrol varsa hepsi mevcut). Aşağıdaki fonksiyonu /* SOMETHING */ yerine ne yazılacak ?

inline bool exist(const std::string& name)
{
    /* SOMETHING */
}

CEVAP
8 EKİM 2012, PAZARTESİ


Birlikte bu yöntemlerin her biri 100,000 kez koştu bir test programı, varolan dosyalar üzerinde yarım yapmadı dosyaları yarısını attım.

#include <sys/stat.h>
#include <unistd.h>

inline bool exists_test0 (const std::string& name) {
    ifstream f(name.c_str());
    if (f.good()) {
        f.close();
        return true;
    } else {
        f.close();
        return false;
    }   
}

inline bool exists_test1 (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }   
}

inline bool exists_test2 (const std::string& name) {
    return ( access( name.c_str(), F_OK ) != -1 );
}

inline bool exists_test3 (const std::string& name) {
  struct stat buffer;   
  return (stat (name.c_str(), &buffer) == 0); 
}

100.000 çağrıları üzerinden 5 çalışır ortalama, çalıştırmak için toplam süre için sonuçlar

Method exists_test0 (ifstream): **0.485s**
Method exists_test1 (FILE fopen): **0.302s**
Method exists_test2 (posix access()): **0.202s**
Method exists_test3 (posix stat()): **0.134s**

Stat() fonksiyonu benim sistem (), g ile derlenmiş Linux üzerinde en iyi performansı, standart fopen bir ara nedense POSIX işlevleri kullanmayı reddetme yapabileceğiniz en iyi olmak şartıyla.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • newreleaseblitz

    newreleasebl

    13 Ocak 2010
  • Professor Messer

    Professor Me

    27 NİSAN 2007
  • Smith Micro Graphics

    Smith Micro

    15 Mayıs 2008