Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace CSharpBasic
{
class Program
{
public static string reverseByAPI(string input){
if (string.IsNullOrEmpty(input)) return input;
char[] inputArrary = input.ToCharArray();
Array.Reverse(inputArrary);
return new string(inputArrary);
}
public static string reverseByArray(string input)
{
if (string.IsNullOrEmpty(input)) return input;
int halfLength = input.Length >> 1;
char[] inputArray = input.ToCharArray();
char temp;
for (int i = 0; i < halfLength; i++)
{
temp = inputArray[i];
3611 ticks. ReverseByAPI(Length:1), runs 10000 times.
3581 ticks. ReverseByXor(Length:1), runs 10000 times.
===============
4955 ticks. ReverseByAPI(Length:10), runs 10000 times.
5349 ticks. ReverseByXor(Length:10), runs 10000 times.
===============
9553 ticks. ReverseByAPI(Length:100), runs 10000 times.
27669 ticks. ReverseByXor(Length:100), runs 10000 times.
===============
120702 ticks. ReverseByAPI(Length:1000), runs 10000 times.
package basic.java;
interface Reverse{
public String reverse(String s);
}
class ReverseByArray implements Reverse{
public String reverse(String s){
if (null == s || s.equals("")) return s;
Original String:1234567890
Reversed String: 0987654321(Reverse by array)
Original String:1234567890
Reversed String: 0987654321(Reverse by array 2)
Original String:1234567890
Reversed String: 0987654321(Reverse by api)
--------
Original String:123
Reversed String: 321(Reverse by array)
Original String:123
public final class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton(){
// Do nothing, make sure user can not create object directly
if (null != INSTANCE){
throw new IllegalArgumentException("Already instantiated.");
}
}
public final class Singleton3{
private static class Handler{
private static final Singleton3 INSTANCE = new Singleton3();
}
private Singleton3(){
if (null != Handler.INSTANCE){
throw new IllegalArgumentException("Already instantiated.");
}
}
public class Singleton5 {
private static volatile Singleton5 INSTANCE = null;
private Singleton5(){
if (null != INSTANCE){
throw new IllegalArgumentException("Already instantiated.");
}
}
public static Singleton5 getInstance(){
public enum Singleton6{
INSTANCE;
private Object s = new Object();
// Invoke this like
// Singleton6.INSTANCE.doSomething();
public Object doSomething(){
return s;
}