Skip to content

Instantly share code, notes, and snippets.

@utilForever
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utilForever/98bad184f925ab374308 to your computer and use it in GitHub Desktop.
Save utilForever/98bad184f925ab374308 to your computer and use it in GitHub Desktop.
tolower function comparison
#include <iostream>
#include <string>
#include <chrono>
void tolower1(std::string& s)
{
for (auto& c : s)
{
if (c <= 'Z' && c >= 'A')
c = c - ('Z' - 'z');
else
c = c;
}
}
std::string tolower2(std::string s)
{
for (auto& c : s)
{
if (c <= 'Z' && c >= 'A')
c = c - ('Z' - 'z');
else
c = c;
}
return s;
}
int main()
{
std::string s = "Binary space partitioning is a generic process of recursively dividing a scene into two until the partitioning satisfies one or more requirements. It can be seen as a generalisation of other spatial tree structures such as k-d trees and quadtrees, one where hyperplanes that partition the space may have any orientation, rather than being aligned with the coordinate axes as they are in k-d trees or quadtrees. When used in computer graphics to render scenes composed of planar polygons, the partitioning planes are frequently (but not always) chosen to coincide with the planes defined by polygons in the scene. \
The specific choice of partitioning plane and criterion for terminating the partitioning process varies depending on the purpose of the BSP tree.For example, in computer graphics rendering, the scene is divided until each node of the BSP tree contains only polygons that can render in arbitrary order.When back - face culling is used, each node therefore contains a convex set of polygons, whereas when rendering double - sided polygons, each node of the BSP tree contains only polygons in a single plane.In collision detection or ray tracing, a scene may be divided up into primitives on which collision or ray intersection tests are straightforward. \
Binary space partitioning arose from the computer graphics need to rapidly draw three - dimensional scenes composed of polygons.A simple way to draw such scenes is the painter's algorithm, which produces polygons in order of distance from the viewer, back to front, painting over the background and previous polygons with each closer object. This approach has two disadvantages: time required to sort polygons in back to front order, and the possibility of errors in overlapping polygons. Fuchs and co-authors[2] showed that constructing a BSP tree solved both of these problems by providing a rapid method of sorting polygons with respect to a given viewpoint (linear in the number of polygons in the scene) and by subdividing overlapping polygons to avoid errors that can occur with the painter's algorithm.A disadvantage of binary space partitioning is that generating a BSP tree can be time - consuming.Typically, it is therefore performed once on static geometry, as a pre - calculation step, prior to rendering or other realtime operations on a scene.The expense of constructing a BSP tree makes it difficult and inefficient to directly implement moving objects into a tree. \
BSP trees are often used by 3D video games, particularly first - person shooters and those with indoor environments.Game engines utilising BSP trees include the Doom engine(probably the earliest game to use a BSP data structure was Doom), the Quake engine and its descendants.In video games, BSP trees containing the static geometry of a scene are often used together with a Z - buffer, to correctly merge movable objects such as doors and characters onto the background scene.While binary space partitioning provides a convenient way to store and retrieve spatial information about polygons in a scene, it does not solve the problem of visible surface determination. \
1969 Schumacker et al.[1] published a report that described how carefully positioned planes in a virtual environment could be used to accelerate polygon ordering. The technique made use of depth coherence, which states that a polygon on the far side of the plane cannot, in any way, obstruct a closer polygon. This was used in flight simulators made by GE as well as Evans and Sutherland. However, creation of the polygonal data organization was performed manually by scene designer. \
1980 Fuchs et al.[2] extended Schumacker’s idea to the representation of 3D objects in a virtual environment by using planes that lie coincident with polygons to recursively partition the 3D space.This provided a fully automated and algorithmic generation of a hierarchical polygonal data structure known as a Binary Space Partitioning Tree(BSP Tree).The process took place as an off - line preprocessing step that was performed once per environment / object.At run - time, the view - dependent visibility ordering was generated by traversing the tree. \
1981 Naylor's Ph.D thesis containing a full development of both BSP trees and a graph-theoretic approach using strongly connected components for pre-computing visibility, as well as the connection between the two methods. BSP trees as a dimension independent spatial search structure was emphasized, with applications to visible surface determination. The thesis also included the first empirical data demonstrating that the size of the tree and the number of new polygons was reasonable (using a model of the Space Shuttle). \
1983 Fuchs et al.describe a micro - code implementation of the BSP tree algorithm on an Ikonas frame buffer system.This was the first demonstration of real - time visible surface determination using BSP trees. \
1987 Thibault and Naylor[3] described how arbitrary polyhedra may be represented using a BSP tree as opposed to the traditional b - rep(boundary representation).This provided a solid representation vs.a surface based - representation.Set operations on polyhedra were described using a tool, enabling Constructive Solid Geometry(CSG) in real - time.This was the fore runner of BSP level design using brushes, introduced in the Quake editor and picked up in the Unreal Editor. \
1990 Naylor, Amanatides, and Thibault provide an algorithm for merging two BSP trees to form a new BSP tree from the two original trees.This provides many benefits including : combining moving objects represented by BSP trees with a static environment(also represented by a BSP tree), very efficient CSG operations on polyhedra, exact collisions detection in O(log n * log n), and proper ordering of transparent surfaces contained in two interpenetrating objects(has been used for an x - ray vision effect). \
1990 Teller and Séquin proposed the offline generation of potentially visible sets to accelerate visible surface determination in orthogonal 2D environments. \
1991 Gordon and Chen[CHEN91] described an efficient method of performing front - to - back rendering from a BSP tree, rather than the traditional back - to - front approach.They utilised a special data structure to record, efficiently, parts of the screen that have been drawn, and those yet to be rendered.This algorithm, together with the description of BSP Trees in the standard computer graphics textbook of the day(Computer Graphics : Principles and Practice) was used by John Carmack in the making of Doom. \
1992 Teller’s PhD thesis described the efficient generation of potentially visible sets as a pre - processing step to accelerate real - time visible surface determination in arbitrary 3D polygonal environments.This was used in Quake and contributed significantly to that game's performance. \
1993 Naylor answers the question of what characterizes a good BSP tree.He used expected case models(rather than worst case analysis) to mathematically measure the expected cost of searching a tree and used this measure to build good BSP trees.Intuitively, the tree represents an object in a multi - resolution fashion(more exactly, as a tree of approximations).Parallels with Huffman codes and probabilistic binary search trees are drawn. \
1993 Hayder Radha's PhD thesis described (natural) image representation methods using BSP trees. This includes the development of an optimal BSP-tree construction framework for any arbitrary input image. This framework is based on a new image transform, known as the Least-Square-Error (LSE) Partitioning Line (LPE) transform. H. Radha' thesis also developed an optimal rate - distortion(RD) image compression framework and image manipulation approaches using BSP trees.";
std::cout << "tolower comparison" << std::endl;
std::cout << "1. void tolower1(std::string& s)" << std::endl;
auto start = std::chrono::steady_clock::now();
tolower1(s);
auto end = std::chrono::steady_clock::now();
std::cout << "Measure time: " << std::chrono::duration<double, std::milli>(end - start).count() << "ms" << std::endl;
std::cout << "2. std::string tolower1(std::string s)" << std::endl;
start = std::chrono::steady_clock::now();
tolower2(s);
end = std::chrono::steady_clock::now();
std::cout << "Measure time: " << std::chrono::duration<double, std::milli>(end - start).count() << "ms" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment