public class PerformanceTest { private static String bigString = "In 1960 Yuri Artsutanov, a young engineer living in Leningrad, published a paper on what he termed a \"heavenly funicular\". This was the first modern description of what is now often referred to as a \"space elevator\". What is a space elevator, you ask? It's quite simple: first put a space station in geosynchronous orbit (GEO), preferably above an ocean or very tall mountain; next, extend equal lengths of tether down towards Earth and in the opposite direction, out into space; when you have extended the tether all the way down to your ocean platform or the top of your mountain, you have a space elevator. The equal lengths of tether extended in opposite directions keep the station's center of gravity in GEO and you have, literally, a rope hanging from the heavens. " + "Once you have your heavenly funicular, it is a relatively simple matter to attach an elevator car to it. Electromagnetic (EM) propulsion is preferable because it removes the need for moving parts that can wear out or require repair. EM propulsion also allows you to reach the fantastic speeds needed to make the trip bearable. It is more than 35,000 kilometers from the Earth's surface to GEO. Using mechanical propulsion, the best car speed we can expect is a couple hundred kilometers per hour; with EM propulsion, a car speed of ten to twenty times that is possible. The trip each way will last between 12 and 16 hours and be devoid of the traumatic acceleration today's astronauts must endure. Using an EM propulsion system also allows the car to be extremely efficient. Solar panels can collect enough energy to power them, and you can even design systems which recapture most of energy used to accelerate the cars when they are decelerated at their destination. " + "Does all this sound impossible? Until very recently, it has been. The problem is an obvious one: your tether will break under its own weight long before it reaches the Earth's surface. One way to combat this is to taper your tether - make it thicker at the top than at the bottom. Unfortunately, even one of the strongest materials available - steel - would have to be several billion kilometers thick at the top to support its own weight. In fact, it would have a mass many times greater than that of the entire solar system put together. If you use Kevlar, a material roughly three times stronger, matters improve, but not much. Can we do better? Some people think yes, and experiments may have demonstrated so already. " + "Enter carbon nanotubes. Carbon is abundant on Earth. It varies in form from the common graphite, soft and fragile, in your pencil, to diamond lattices that make up one of the strongest materials known to man. Nanotubes are another form, one that doesn't occur in nature. A carbon nanotube is made up of carbon atoms arranged into a cylindrical shape, with walls a single atom thick, and ends capped with half-soccer balls. Scientists make them in a variety of ways. One way involves blasting graphite with a high power laser and letting the resulting carbon vapor arrange itself around various substrates; another mixes carbon with oxygen under high pressure and then slowly deposits the vapors to build up nanotubes. " + "But all existing methods are expensive. It costs about $500 USD to make a single gram of carbon nanotubes, and it will take nearly 15 million kilograms of the stuff to build a space elevator. For reference, that's about 7,700 shuttle payloads, or 154 years worth of dedicated launches. Several companies think they can bring the cost down to as little as $0.05 USD per gram in the next 5 to 10 years. But how can we carry so much material into space? " +"The answer lies in a modification of the design of the space elevator. Remember, equal weights must be extended from each side of the station or its center of gravity will change and it will fall to Earth or tumble out into space. There is an alternative to weighing the space-end of the station with tether, though. A counter-weight asteroid with the right mass can be used in place of the tether. This asteroid can also be mined for the carbon to make nanotubes, even the most average of carbonaceous asteroids has enough carbon to spare for such a task. Such asteroids are readily available and waiting in the Earth-Sun LaGrange points. " + "There are plenty of reasons for us to build a space elevator. Today, space flight is extremely expensive; it costs about $20,000 USD to launch a single kilogram into orbit. Most existing launch programs have a narrow focus for which they work well: launching comsats, launching unmanned probes, or carrying people into orbit. While the systems are adaptable to other uses, such adaptation is expensive. With a space elevator, any mission you can think of can be launched without the least bit of trouble. Unmanned probes? Trivial, just fling them off the outer end of the tether. Their tangential velocity will be great enough to send them on unpowered trips all the way to Saturn. The situation is similar with comsats; just drop them off the tether a bit sooner, and put them in an orbit that won't later collide with the space elevators. Manned missions are a cinch - being able to lift consumables and equipment at a miniscule fraction of the cost (estimates range from $0.50 to $2.50 USD per kilogram, or 0.001% of current costs) opens up a world of possibilities. What about a manned mission to Mars? Easy. A fully equipped moon base, complete with the latest state of the art astronomical equipment? Piece of cake. " + "There are some problems to overcome before a space elevator can be made a reality. Within the decade we will be able to make cheap nanotubes on Earth, but what about in space? Can they be synthesized from carbon extracted from an asteroid? Speaking of asteroids, moving one from a LaGrange point into orbit will be no small task. While we possess the technology now - a combination of light sails, ion propulsion, and good old fashion chemical rockets could do it - the cost will be great, and the precision required fantastic. Missing Earth orbit could have disastrous results, and is not an option. Arthur C. Clarke wrote, \"the space elevator will be built about fifty years after everyone stops laughing.\" Are we ready to stop laughing and turn this age-old myth into reality? "; private static int bigStringLength = bigString.length(); private String testString = null; private int iters = 0; private int repeats = 0; private long toCharArray = 0; private long charAt = 0; private PerformanceTest(int stringLength, int iters, int repeats) { if (stringLength > bigStringLength) throw new RuntimeException("Requested string length " + stringLength + " too long"); testString = bigString.substring(0, stringLength); this.iters = iters; this.repeats = repeats; } private void test() { toCharArray = testToCharArray(); charAt = testCharAt(); } public String toString() { return " " + iters + " " + repeats + " stringLen " + testString.length() + " toChar " + toCharArray + " charAt " + charAt; } private long testToCharArray() { long timings[] = new long[repeats]; for (int i = 0; i < repeats + 1; ++i) { int count = 0; long then = System.currentTimeMillis(); for (int j = 0; j < iters; ++j) { int len = testString.length(); char[] chars = testString.toCharArray(); for (int k = 0; k < len; ++k) { if (chars[k] == ' ') ++count; } } long now = System.currentTimeMillis(); if (i > 0) timings[i-1] = now - then; } long totalTime = 0; for (int i = 0; i < repeats; ++i) { totalTime += timings[i]; } return totalTime / repeats; } private long testCharAt() { long timings[] = new long[repeats]; for (int i = 0; i < repeats + 1; ++i) { int count = 0; long then = System.currentTimeMillis(); for (int j = 0; j < iters; ++j) { int len = testString.length(); for (int k = 0; k < len; ++k) { if (testString.charAt(k) == ' ') ++count; } } long now = System.currentTimeMillis(); if (i > 0) timings[i-1] = now - then; } long totalTime = 0; for (int i = 0; i < repeats; ++i) { totalTime += timings[i]; } return totalTime / repeats; } public static void main(String args[]) { int iters = Integer.parseInt(args[0]); int repeats = Integer.parseInt(args[1]); int startLen = Integer.parseInt(args[2]); int lenIncr = Integer.parseInt(args[3]); int lenCount = Integer.parseInt(args[4]); System.out.println("Java Version " + System.getProperty("java.version")); System.out.println("Java Specification Version " + System.getProperty("java.specification.version")); System.out.println("Java VM Version " + System.getProperty("java.vm.version")); System.out.println("Java VM Specification Version " + System.getProperty("java.vm.specification.version")); System.out.println("Java Compiler " + System.getProperty("java.compiler")); int len = startLen; for (int i = 0; i < lenCount; ++i) { PerformanceTest pt = new PerformanceTest(len, iters, repeats); pt.test(); System.out.println(pt.toString()); len += lenIncr; } } }