Why is it that FileInputStream read is slower with bigger array

后端 未结 4 1595
盖世英雄少女心
盖世英雄少女心 2020-12-31 18:31

If I read bytes from a file into a byte[] I see that FileInputStream performance worse when the array is around 1 MB compared to 128 KB. On the 2 workstations I have tested

4条回答
  •  盖世英雄少女心
    2020-12-31 18:42

    This can be because of cpu cache,

    cpu has its own cache memory and there is some fix size for that you can check your cpu cache size by executing this command on cmd

    wmic cpu get L2CacheSize

    suppose you have 256k as cpu cache size, So what happens is If you read 256k chunks or smaller, the content that was written to the buffer is still in the CPU cache when the read accesses it. If you have chunks greater of 256k then the last 256k that were read are in the CPU cache, so when the read starts from the beginning the content must be retrieved from main memory.

提交回复
热议问题