/
size_comparison_A.go
173 lines (141 loc) · 3.43 KB
/
size_comparison_A.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bytes"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"gopkg.in/mgo.v2/bson"
"os"
"github.com/ugorji/go/codec"
)
// Vector represents type of data to be serialized into various formats
type Vector []float64
func encodeVectorIntoBSON(vector Vector) ([]byte, error) {
bsonOutput, err := bson.Marshal(vector)
if err != nil {
return bsonOutput, err
}
return bsonOutput, nil
}
func encodeVectorIntoJSON(vector Vector) ([]byte, error) {
jsonOutput, err := json.Marshal(vector)
if err != nil {
return jsonOutput, err
}
return jsonOutput, nil
}
func encodeVectorIntoIndentedJSON(vector Vector) ([]byte, error) {
jsonOutput, err := json.MarshalIndent(vector, "", " ")
if err != nil {
return jsonOutput, err
}
return jsonOutput, nil
}
func encodeVectorIntoXML(vector Vector) ([]byte, error) {
xmlOutput, err := xml.Marshal(vector)
if err != nil {
return xmlOutput, err
}
return xmlOutput, nil
}
func encodeVectorIntoIndentedXML(vector Vector) ([]byte, error) {
xmlOutput, err := xml.MarshalIndent(vector, "", " ")
if err != nil {
return xmlOutput, err
}
return xmlOutput, nil
}
func encodeVectorIntoGob(vector Vector) ([]byte, error) {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(vector)
if err != nil {
return buffer.Bytes(), err
}
return buffer.Bytes(), nil
}
func encodeVectorIntoMsgPack(vector Vector) ([]byte, error) {
var buffer bytes.Buffer
// handler
var handler codec.MsgpackHandle
// objekt realizující zakódování dat
encoder := codec.NewEncoder(&buffer, &handler)
// zakódování dat
err := encoder.Encode(vector)
if err != nil {
return buffer.Bytes(), err
}
return buffer.Bytes(), nil
}
func saveVector(encodedVector []byte, filename string) {
err := os.WriteFile(filename, encodedVector, 0o644)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Stored into file", filename)
}
}
func printBufferInfo(buffer []byte) {
fmt.Println("\nBuffer with encoded vector: ", len(buffer))
}
func main() {
var array [1000]float64
for i := 0; i < len(array); i++ {
if i == 0 {
array[i] = 1.0
} else {
array[i] = 1.0 / float64(i)
}
}
var vector Vector = array[:]
encodedVector, err := encodeVectorIntoXML(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector1.xml")
encodedVector, err = encodeVectorIntoIndentedXML(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector2.xml")
encodedVector, err = encodeVectorIntoJSON(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector1.json")
encodedVector, err = encodeVectorIntoIndentedJSON(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector2.json")
encodedVector, err = encodeVectorIntoBSON(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector1.bson")
encodedVector, err = encodeVectorIntoGob(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector1.gob")
encodedVector, err = encodeVectorIntoMsgPack(vector)
if err != nil {
fmt.Println(err)
return
}
printBufferInfo(encodedVector)
saveVector(encodedVector, "/tmp/vector1.bin")
}