Project

General

Profile

Patch #967 » bench.cpp

Michael Pratt, 05 Jul 2014 19:14

 
1
#include <omp.h>
2

    
3
#include <iostream>
4
#include <iomanip>
5
#include <cassert>
6
#include <vector>
7

    
8
#include "exiv2.hpp"
9

    
10
void check_exif(Exiv2::Exifdatum *datum, Exiv2::ExifKey expected)
11
{
12
    if (!datum) {
13
        std::cout << "Key not found! (" << expected.key() << ")\n";
14
    } else if (datum->key() != expected.key()) {
15
        std::cout << "Wrong key, expected " << expected.key() << " (" << expected.tag() << ") found " << datum->key() << " (" << datum->tag() << ")\n";
16
    }
17
}
18

    
19
void time_exif(Exiv2::Image::AutoPtr *image) {
20
    Exiv2::ExifData &exifData = (*image)->exifData();
21
    if (exifData.empty()) {
22
        std::cout << "No Exif data\n";
23
        return;
24
    }
25

    
26
    std::vector<Exiv2::ExifKey> keys;
27

    
28
    Exiv2::ExifData::const_iterator exif_end = exifData.end();
29
    for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != exif_end; ++i) {
30
        std::string key = i->key();
31
        keys.push_back(Exiv2::ExifKey(key));
32
    }
33

    
34
    double start_time = omp_get_wtime();
35

    
36
    std::vector<Exiv2::ExifKey>::const_iterator keys_end = keys.end();
37
    for (std::vector<Exiv2::ExifKey>::const_iterator i = keys.begin(); i != keys_end; ++i) {
38
        Exiv2::Exifdatum datum = *exifData.findKey(*i);
39
        check_exif(&datum, *i);
40
    }
41

    
42
    double end_time = omp_get_wtime();
43

    
44
    // milliseconds
45
    double elapsed = 1000*(end_time - start_time);
46

    
47
    std::cout << "Exif:\tTags: " << keys.size() << "\tLookup time: " << elapsed << "ms\n";
48
}
49

    
50
void check_xmp(Exiv2::Xmpdatum *datum, Exiv2::XmpKey expected)
51
{
52
    if (!datum) {
53
        std::cout << "Key not found! (" << expected.key() << ")\n";
54
    } else if (datum->key() != expected.key()) {
55
        std::cout << "Wrong key, expected " << expected.key() << " found " << datum->key() << "\n";
56
    }
57
}
58

    
59
void time_xmp(Exiv2::Image::AutoPtr *image) {
60
    Exiv2::XmpData &xmpData = (*image)->xmpData();
61
    if (xmpData.empty()) {
62
        std::cout << "No Xmp data\n";
63
        return;
64
    }
65

    
66
    std::vector<Exiv2::XmpKey> keys;
67

    
68
    Exiv2::XmpData::const_iterator xmp_end = xmpData.end();
69
    for (Exiv2::XmpData::const_iterator i = xmpData.begin(); i != xmp_end; ++i) {
70
        std::string key = i->key();
71
        keys.push_back(Exiv2::XmpKey(key));
72
    }
73

    
74
    double start_time = omp_get_wtime();
75

    
76
    std::vector<Exiv2::XmpKey>::const_iterator keys_end = keys.end();
77
    for (std::vector<Exiv2::XmpKey>::const_iterator i = keys.begin(); i != keys_end; ++i) {
78
        Exiv2::Xmpdatum datum = *xmpData.findKey(*i);
79
        check_xmp(&datum, *i);
80
    }
81

    
82
    double end_time = omp_get_wtime();
83

    
84
    // milliseconds
85
    double elapsed = 1000*(end_time - start_time);
86

    
87
    std::cout << "Xmp:\tTags: " << keys.size() << "\tLookup time: " << elapsed << "ms\n";
88
}
89

    
90
void check_iptc(Exiv2::Iptcdatum *datum, Exiv2::IptcKey expected)
91
{
92
    if (!datum) {
93
        std::cout << "Key not found! (" << expected.key() << ")\n";
94
    } else if (datum->key() != expected.key()) {
95
        std::cout << "Wrong key, expected " << expected.key() << " (" << expected.tag() << ") found " << datum->key() << " (" << datum->tag() << ")\n";
96
    }
97
}
98

    
99
void time_iptc(Exiv2::Image::AutoPtr *image) {
100
    Exiv2::IptcData &iptcData = (*image)->iptcData();
101
    if (iptcData.empty()) {
102
        std::cout << "No Iptc data\n";
103
        return;
104
    }
105

    
106
    std::vector<Exiv2::IptcKey> keys;
107

    
108
    Exiv2::IptcData::const_iterator iptc_end = iptcData.end();
109
    for (Exiv2::IptcData::const_iterator i = iptcData.begin(); i != iptc_end; ++i) {
110
        std::string key = i->key();
111
        keys.push_back(Exiv2::IptcKey(key));
112
    }
113

    
114
    double start_time = omp_get_wtime();
115

    
116
    std::vector<Exiv2::IptcKey>::const_iterator keys_end = keys.end();
117
    for (std::vector<Exiv2::IptcKey>::const_iterator i = keys.begin(); i != keys_end; ++i) {
118
        Exiv2::Iptcdatum datum = *iptcData.findKey(*i);
119
        check_iptc(&datum, *i);
120
    }
121

    
122
    double end_time = omp_get_wtime();
123

    
124
    // milliseconds
125
    double elapsed = 1000*(end_time - start_time);
126

    
127
    std::cout << "Iptc:\tTags: " << keys.size() << "\tLookup time: " << elapsed << "ms\n";
128
}
129

    
130
int main(int argc, char* const argv[]) {
131
    try {
132
        if (argc != 2) {
133
            std::cout << "Usage: " << argv[0] << " file\n";
134
            return 1;
135
        }
136

    
137
        Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
138
        assert(image.get() != 0);
139
        image->readMetadata();
140

    
141
        time_exif(&image);
142
        time_xmp(&image);
143
        time_iptc(&image);
144

    
145
        return 0;
146
    }
147
    catch (Exiv2::Error& e) {
148
        std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
149
        return -1;
150
    }
151
}
(7-7/7)