iOS圧縮アルゴリズムの簡単な比較

3000 ワード

LAFSEとMiniLZO、システムに付属のzlibを簡単に使用しました
重複データが非常に大きい場合にはLAFSEの表現が優れ,291 wバイトで3.2 k程度に圧縮できることが分かった.
曲(mp 3形式)を探してデータテストを読み出した場合、MiniLZOの圧縮率はLZFSEよりやや高い
呼び出しメソッドパッケージ⬇️
func lzfse_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            let res = lzfse_encode_buffer(toBytes, dst.count, fromBytes, src.count, nil)
            if res > 0{
                dst = dst[0...res]
            }
            print("res == \(res)")
        }
    }
}

func lzfse_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            let res = lzfse_decode_buffer(toBytes, dst.count, fromBytes, src.count, nil)
            print("res == \(res)")
        }
    }
}

func minilzo_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            var length = lzo_uint(dst.count)
            let wkrmem = UnsafeMutableRawPointer.allocate(bytes: src.count, alignedTo: 0)
            let res = lzo1x_1_compress(fromBytes, lzo_uint(src.count), toBytes, &length, wkrmem)
            if res == LZO_E_OK{
                print("res == \(length)")
            }
        }
    }
}

func minilzo_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            var length = lzo_uint(src.count)
            let res = lzo1x_decompress(fromBytes, lzo_uint(src.count), toBytes, &length, nil)
            if res == LZO_E_OK{
                print("res == \(length)")
            }
        }
    }
}

func zlib_encode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            var length = uLongf(src.count)
            let res = compress(toBytes, &length, fromBytes, uLong(src.count))
            if res == Z_OK{
                print("res == \(length)")
            }
        }
    }
}

func zlib_decode(src:Data,dst:inout Data){
    src.withUnsafeBytes { (fromBytes: UnsafePointer) -> Void in
        dst.withUnsafeMutableBytes{ (toBytes: UnsafeMutablePointer) -> Void in
            var length = uLongf(dst.count)
            let res = uncompress(toBytes, &length, fromBytes, uLong(src.count))
            if res == Z_OK{
                print("res == \(length)")
            }
        }
    }
}