2016-04-05 40 views
0

所以今天我瞭解了kotlin中的num.inc()特性,並決定在我的代碼中實現它。不用說,那加10倍的等待時間,以我的代碼(從大約400毫秒去4000 + MS)Kotlin i.inc()比i ++慢10倍?

這裏是我的傳統方式的例子(我++,400毫秒)

package com.beaudoin 

import java.io.BufferedWriter 
import java.io.FileInputStream 
import java.io.FileWriter 
import java.nio.channels.FileChannel 

fun main(args: Array<String>) { 
    val s = System.currentTimeMillis() 

    val channel = FileInputStream("client.dll").channel 
    val buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()) 
    val data = ByteArray(buffer.capacity()) 
    buffer.get(data) 

    val writer = BufferedWriter(FileWriter("dump.txt", false)) 
    val bytes = ByteArray(16) 

    var offset = 0 
    var i = 0 
    while (i < data.size) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i++] 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    } 

    writer.close() 

    println(System.currentTimeMillis() - s) 
} 

private val HEX_ARRAY = "ABCDEF".toCharArray() 
private val bytes = ByteArray(4); 

class HexRow(val offset: Int, val values: ByteArray) { 

    fun bytesToChar(bytes: ByteArray, width: Int): CharArray { 
     val hexChars = CharArray((bytes.size * 2) + (bytes.size/width)) 

     for (i in bytes.indices) { 
      val v = bytes[i].toInt() and 0xFF 
      val idx = (i * 2) + i/width 

      hexChars[idx] = HEX_ARRAY[v.ushr(4)] 
      hexChars[idx + 1] = HEX_ARRAY[v and 0x0F] 
      if (idx + 2 < hexChars.size) { 
       hexChars[idx + 2] = ' ' 
      } 
     } 
     return hexChars; 
    } 

    fun bytesToHex(value: Int) = String(bytesToChar(toByteArray(value), 6)) 

    fun bytesToHex(bytes: ByteArray) = String(bytesToChar(bytes, 1)) 

    fun toByteArray(value: Int): ByteArray { 
     bytes[0] = value.ushr(24).toByte() 
     bytes[1] = value.ushr(16).toByte() 
     bytes[2] = value.ushr(8).toByte() 
     bytes[3] = value.toByte() 
     return bytes 
    } 

    override fun toString() = bytesToHex(offset) + " " + bytesToHex(values) 

} 

,這裏是使用i.inc()(4000ms +)的代碼

package com.beaudoin 

import java.io.BufferedWriter 
import java.io.FileInputStream 
import java.io.FileWriter 
import java.nio.channels.FileChannel 

fun main(args: Array<String>) { 
    val s = System.currentTimeMillis() 

    val channel = FileInputStream("client.dll").channel 
    val buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()) 
    val data = ByteArray(buffer.capacity()) 
    buffer.get(data) 

    val writer = BufferedWriter(FileWriter("dump.txt", false)) 
    val bytes = ByteArray(16) 

    var offset = 0 
/* var i = 0 
    while (i < data.size) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i++] 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    }*/ 

    for (i in data.indices) { 
     for (j in bytes.indices) { 
      bytes[j] = data[i] 
      i.inc() 
     } 
     writer.write(HexRow(offset, bytes).toString()) 
     writer.newLine() 
     offset += 16 
    } 

    writer.close() 

    println(System.currentTimeMillis() - s) 
} 

private val HEX_ARRAY = "ABCDEF".toCharArray() 
private val bytes = ByteArray(4); 

class HexRow(val offset: Int, val values: ByteArray) { 

    fun bytesToChar(bytes: ByteArray, width: Int): CharArray { 
     val hexChars = CharArray((bytes.size * 2) + (bytes.size/width)) 

     for (i in bytes.indices) { 
      val v = bytes[i].toInt() and 0xFF 
      val idx = (i * 2) + i/width 

      hexChars[idx] = HEX_ARRAY[v.ushr(4)] 
      hexChars[idx + 1] = HEX_ARRAY[v and 0x0F] 
      if (idx + 2 < hexChars.size) { 
       hexChars[idx + 2] = ' ' 
      } 
     } 
     return hexChars; 
    } 

    fun bytesToHex(value: Int) = String(bytesToChar(toByteArray(value), 6)) 

    fun bytesToHex(bytes: ByteArray) = String(bytesToChar(bytes, 1)) 

    fun toByteArray(value: Int): ByteArray { 
     bytes[0] = value.ushr(24).toByte() 
     bytes[1] = value.ushr(16).toByte() 
     bytes[2] = value.ushr(8).toByte() 
     bytes[3] = value.toByte() 
     return bytes 
    } 

    override fun toString() = bytesToHex(offset) + " " + bytesToHex(values) 

} 

有人可以告訴我爲什麼i.inc()比i ++慢得多嗎?

PS:與任何文件替換client.dll(應該是12MB〜讓數字精確到礦山或者乾脆下載我的測試文件here

回答

7

inc()在代碼中調用是完全多餘的:它不會改變變量:

var x = 0 
x.inc() 
println(x) // 0 

作爲在運行時間的差,在兩個實施方式中的連語義不同的是:

  • 第一個片段:

    while (i < data.size) { 
        for (j in bytes.indices) { 
         bytes[j] = data[i++] 
        } 
        //... 
    } 
    

    內環改變i,因此每個i也不會有的while迭代的i一些(甚至大部分,我想)值將被跳過。

  • 二片段:

    for (i in data.indices) { 
        for (j in bytes.indices) { 
         bytes[j] = data[i] // removed redundant i.inc() 
        } 
        //... 
    } 
    

    內循環呼籲在外環各i,因此它會需要更長的時間。

+0

啊沒有明白。謝謝! –

相關問題