1. 程式人生 > >Java效能提示(全)

Java效能提示(全)

  • AWT components are not useful as game actors (sprites) as they do not overlap well, nor are they good at being moved around the screen.
  • Celled image files efficiently store an animated image by dividing an image into a rectangular grid of cells, and allocating a different animation image to each cell. A sequence of similar images (as you would have for an animation) will be stored and transferred efficiently in most image formats.
  • Examining pixels using PixelGrabber is slow.
  • drawImage() can throw away and re-load images in response to memory requirements, which can make things slow.
  • Pre-load and pre-scale images before using them to get a smoother and faster display.
  • The more actors (sprites), the more time it takes to draw and the slower the game appears.
  • Use double-buffering to move actors (sprites), by redrawing the actor and background for the relevant area.
  • Redraw speed depends on: how quickly each object is drawn; how many objects are drawn; how much of each object is drawn; the total number of drawing operations. You need to reduce some or all of these until you get to about 30 redraws per second.
  • Don't draw actors or images that cannot be seen.
  • If an actor is not moving then incorporate the actor as part of the background.
  • Only redraw the area that has changed, e.g. the old area where an actor was, and the new area where it is. Redrawing several small areas is frequently faster than drawing one large area. For the redraws, eliminate overlapping areas and merge adjacent (close) areas so that the number of redraws is kept to a minimum.
  • Put slow and fast drawing requirements in separate threads.
  • Bounding-box detection can use circles for the bounding box which requires a simple radii detection.
  • Load sounds in a background thread.
  • Make sure you have a throttle control that can make the game run slower (or pause) when necessary.
  • The optimal network topology for network games depends on the number of users.
  • If the cumulative downloading of your applet exceeds the player?s patience, you?ve lost a customer.
  • The user interface should always be responsive. A non-responsive window means you will lose your players. Give feedback on necessary delays. Provide distractions when unavoidable delays will be lengthy [more than a few seconds].
  • Transmission time varies, and is always slow compared to operations on the local hardware. You may need to decide the outcome of the action locally, then broadcast the result of the action. This may require some synchronization resolution.
  • Latency between networked players can easily lead to de-synchronized action and player frustration. Displays should locally simulate remote action as continuing current activities/motions, until the display is updated. On update, the actual current situation should be smoothly resolved with the simulated current situation.
  • Sending activity updates more frequently ensures smoother play and better synchronization between networked players, but requires more CPU effort and so affects the local display. In order to avoid adversely affecting local displays, send actvity updates from a low priority thread.
  • Discard any out-of-date updates: always use the latest dated update.
  • A minimum broadcast delay of one-third the average network connection travel time is appropriate. Once you exceed this limit, the additional traffic can cause more grief than benefit.
  • Put class files into a (compressed) container for network downloading.
  • Avoid repeatedly evaluating invariant expressions in a loop.
  • Take advantage of inlining where possible (using final, private and static keywords, and compiling with javac -O)
  • Profile the code to determine the expensive methods (e.g. using the -prof option)
  • Use a dissassembler (e.g. like javap) to determine which of various alternative coding formulations produces smaller bytecode.
  • To reduce the number of class files and their sizes: use the SDK classes as much as possible; and implement common functionality in one place only.
  • To optimize speed: avoid synchronized methods; use buffered I/O; reuse objects; avoid unnecessary screen painting.
  • Raycasting is faster than raytracing. Raycasting maps 2D data into a 3D world, drawing entire vertical lines using one ray. Use precalculated values for trignometric and other functions, based on the angle increments chosen for your raycasting.
  • In the absence of a JIT, the polygon drawing routines fron the AWT are relatively efficient (compared to array manipulation) and may be faster than texture mapping.
  • Without texture mapping, walls can be drawn faster with one call to fillPolygon (rather than line by line).
  • An exponential jump search algorithm can be used to reduce ray casts - by quickly finding boundaries where walls end (like a binary search, but double increments until your overshoot, then halving increments from the last valid wall position).
  • It is usually possible to increase performance at the expense of image quality and accuracy. Techniques include reducing pixel depth or display resolution, field interlacing, aliasing. The key, however, is to degrade the image in a way that is likely to be undetectable or unnoticeable to the user. For example a moving player often pays less attention to image quality than a resting or static player.
  • Use information gathered during the rendering of one frame to approximate the geometry of the next frame, speeding up its rendering.
  • If the geometry and content is not too complicated, binary space partition trees map the view according to what the player can see, and can be faster than ray casting.