Пример #1
0
func (this *BWTBlockCodec) createGST(blockSize uint) (kanzi.ByteTransform, error) {
	// SBRT can perform MTFT but the dedicated class is faster
	if this.mode == GST_MODE_RAW {
		return nil, nil
	}

	if this.mode == GST_MODE_MTF {
		return transform.NewMTFT(blockSize)
	}

	return transform.NewSBRT(this.mode, blockSize)
}
Пример #2
0
func main() {
	fmt.Printf("\nCorrectness test")

	for ii := 0; ii < 20; ii++ {
		rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
		var input []byte
		if ii == 0 {
			input = []byte{5, 2, 4, 7, 0, 0, 7, 1, 7}
		} else {
			input = make([]byte, 32)

			for i := 0; i < len(input); i++ {
				input[i] = byte(65 + rnd.Intn(5*ii))
			}
		}

		size := len(input)
		mtft, _ := transform.NewMTFT(uint(size))
		transform := make([]byte, size+20)
		reverse := make([]byte, size)

		fmt.Printf("\nTest %d", (ii + 1))
		fmt.Printf("\nInput     : ")

		for i := 0; i < len(input); i++ {
			fmt.Printf("%d ", input[i])
		}

		start := (ii & 1) * ii
		mtft.Forward(input, transform[start:])
		fmt.Printf("\nTransform : ")

		for i := start; i < start+len(input); i++ {
			fmt.Printf("%d ", transform[i])
		}

		mtft.Inverse(transform[start:], reverse)
		fmt.Printf("\nReverse   : ")

		for i := 0; i < len(input); i++ {
			fmt.Printf("%d ", reverse[i])
		}

		fmt.Printf("\n")
		ok := true

		for i := 0; i < len(input); i++ {
			if reverse[i] != input[i] {
				ok = false
				break
			}
		}

		if ok == true {
			fmt.Printf("Identical\n")
		} else {
			fmt.Printf("Different\n")
		}
	}

	// Speed Test
	iter := 20000
	size := 10000
	fmt.Printf("\n\nSpeed test")
	fmt.Printf("\nIterations: %v", iter)

	for jj := 0; jj < 4; jj++ {
		input := make([]byte, size)
		output := make([]byte, size)
		reverse := make([]byte, size)
		mtft, _ := transform.NewMTFT(uint(size))
		delta1 := int64(0)
		delta2 := int64(0)

		if jj == 0 {
			fmt.Printf("\n\nPurely random input")
		}

		if jj == 2 {
			fmt.Printf("\n\nSemi random input")
		}

		for ii := 0; ii < iter; ii++ {
			for i := 0; i < len(input); i++ {
				n := 128

				if jj < 2 {
					// Pure random
					input[i] = byte(rand.Intn(256))
				} else {
					// Semi random (a bit more realistic input)
					rng := 5

					if i&7 == 0 {
						rng = 128
					}

					p := (rand.Intn(rng) - rng/2 + n) & 0xFF
					input[i] = byte(p)
					n = p
				}
			}

			before := time.Now()
			mtft.Forward(input, output)
			after := time.Now()
			delta1 += after.Sub(before).Nanoseconds()
			before = time.Now()
			mtft.Inverse(output, reverse)
			after = time.Now()
			delta2 += after.Sub(before).Nanoseconds()
		}

		idx := -1

		// Sanity check
		for i := range input {
			if input[i] != reverse[i] {
				idx = i
				break
			}
		}

		if idx >= 0 {
			fmt.Printf("Failure at index %v (%v <-> %v)\n", idx, input[idx], reverse[idx])
			os.Exit(1)
		}

		fmt.Printf("\nMTFT Forward transform [ms]: %v", delta1/1000000)
		fmt.Printf("\nThroughput [KB/s]: %d", (int64(iter*size))*1000000/delta1*1000/1024)
		fmt.Printf("\nMTFT Reverse transform [ms]: %v", delta2/1000000)
		fmt.Printf("\nThroughput [KB/s]: %d", (int64(iter*size))*1000000/delta2*1000/1024)
		println()
	}
}