Commit b65ea6b3 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596003777

0 → 120000
+1 −0
Changes for 9780596003777: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596003777
 No newline at end of file

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
  
# Java Performance Tuning 2nd Edition, by Jack Shirazi
  
[![Java Performance Tuning 2nd Edition, by Jack Shirazi](http://akamaicovers.oreilly.com/images/9780596003777/cat.gif)](https://www.safaribooksonline.com/)
  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
+6 KiB

File added.

No diff preview for this file type.

+755 −0

File added.

Preview size limit exceeded, changes collapsed.

+103 −0
Changes for javapt2 Folder/cache/CacheTest.java: 103 added lines, 0 removed lines.
Original line number Diff line number Diff line
package tuning.cache;

import java.util.HashMap;
import java.util.Hashtable;
import java.lang.Math;

public class CacheTest
{
  static Object[] cache_keys = new Object[128];
  static Object[] cache_values = new Object[128];
  static Hashtable hash = new Hashtable();

  public static void main(String[] args)
  {
    try
    {
      System.out.println("started populating");
      populate();
      System.out.println("started accessing");
      access_test();
      access_test();
    }
    catch(Exception e){e.printStackTrace();}
  }

  public static void populate()
  {
    for (int i = 0; i < 100000; i++)
      hash.put(new Integer(i), new Integer(i+5));
  }

  public static Object plain_access(Integer i)
  {
    return hash.get(i);
  }

  public static Object cached_access(Integer i)
  {
    int access = Math.abs(i.hashCode()) & 127;
    Object o;
    if ( (o = cache_keys[access]) == null || !o.equals(i))
    {
      if (o != null)
        System.out.println("Collsion between " + o + " and " + i);
      o = hash.get(i);
      cache_keys[access] = i;
      cache_values[access] = o;
      return o;
    }
    else
    {
      return cache_values[access];
    }
  }

  public static void access_test()
  {
    Integer a0 = new Integer(6767676);
    Integer a1 = new Integer(33);
    Integer a2 = new Integer(998);
    Integer a3 = new Integer(3333);
    Integer a4 = new Integer(12348765);
    Integer a5 = new Integer(9999);
    Integer a6 = new Integer(66665);
    Integer a7 = new Integer(1234);
    Integer a8 = new Integer(987654);
    Integer a9 = new Integer(3121219);
    Object o1,o2,o3,o4,o5,o6,o7,o8,o9,o0;
    long time = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++)
    {
       o1 = plain_access(a0);
       o2 = plain_access(a1);
       o3 = plain_access(a2);
       o4 = plain_access(a3);
       o5 = plain_access(a4);
       o6 = plain_access(a5);
       o7 = plain_access(a6);
       o8 = plain_access(a7);
       o9 = plain_access(a8);
       o0 = plain_access(a9);
    }
    System.out.println("plain access took " + (System.currentTimeMillis()-time));

    time = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++)
    {
       o1 = cached_access(a0);
       o2 = cached_access(a1);
       o3 = cached_access(a2);
       o4 = cached_access(a3);
       o5 = cached_access(a4);
       o6 = cached_access(a5);
       o7 = cached_access(a6);
       o8 = cached_access(a7);
       o9 = cached_access(a8);
       o0 = cached_access(a9);
    }
    System.out.println("cached access took " + (System.currentTimeMillis()-time));

  }

}
Loading